git学习(一)
一、在GitHub上新建一个代码仓库,如gitteach01
二、本地安装git
三、创建本地git仓库
tangxuefengdeMacBook-Pro:~ tangxuefeng$ mkdir gitteach01
tangxuefengdeMacBook-Pro:~ tangxuefeng$ cd gitteach01/
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ echo 1 > 1.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ echo 2 > 2.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git init
Initialized empty Git repository in /Users/tangxuefeng/gitteach01/.git/
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
1.txt
2.txt
nothing added to commit but untracked files present (use "git add" to track)
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git add 1.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
2.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git add 2.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ mkdir dir1
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ echo 3 > dir1/3.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
dir1/
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git add dir1/
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
new file: dir1/3.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ echo 4 > dir1/4.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
new file: dir1/3.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
dir1/4.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git add dir1/4.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 1.txt
new file: 2.txt
new file: dir1/3.txt
new file: dir1/4.txt
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git commit -m "add files and dir"
[master (root-commit) c5502d7] add files and dir
4 files changed, 4 insertions(+)
create mode 100644 1.txt
create mode 100644 2.txt
create mode 100644 dir1/3.txt
create mode 100644 dir1/4.txt
四、推送代码到GitHub
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git remote add origin https://github.com/StevenTangBUPT/gitteach01.git
tangxuefengdeMacBook-Pro:gitteach01 tangxuefeng$ git push -u origin master
Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (7/7), 371 bytes | 371.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/StevenTangBUPT/gitteach01/pull/new/master
remote:
To https://github.com/StevenTangBUPT/gitteach01.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
说明:
[x]运行 git remote add <shortname> <url>
添加一个新的远程 Git 仓库,同时指定一个你可以轻松引用的简写:
$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push)
[x]推送到远程仓库
当你想分享你的项目时,必须将其推送到上游。 这个命令很简单:git push [remote-name] [branch-name]。 当你想要将 master 分支推送到 origin 服务器时(再次说明,克隆时通常会自动帮你设置好那两个名字),那么运行这个命令就可以将你所做的备份到服务器:
$ git push origin master