本章覆盖基本的命令,通过这些命令你可以实现各种各样的任务。
有两种途径:1是将一个存在的项目或目录导入Git,2是从服务器克隆一个存在的Git repository。
初始化仓库到一个存在的目录
1 2 | $ git init Initialized empty Git repository in /work/git/ .git/ |
这将创建一个新的子目录.git,包含所有仓库需要的文件,一个git仓库skeleton。
从一个存在的仓库克隆
1 | $ git clone git: //github .com /schacon/grit .git |
这将创建一个名叫grit的目录,包含一个.git子目录。pull down仓库的所有数据。check out最新版本的工作拷贝。你也可以指定一个目录存放,在命令后面接上目录名即可,如果目录不存在,将自动创建:
1 | $ git clone git: //github .com /schacon/grit .git mygrit |
工作目录中的文件有两种状态:tracked, untracked。
tracked files是那些在最新快照中的文件,它们可以是:unmodified, modified或者staged。
untracked files是其它所有文件,那些你工作目录中不在最后的快照中,也不在staging area中的文件。
当你第一次clone一个仓库时,所有的文件都是tracked,而且是unmodified。
当你编辑文件,Git看到它们是modified,你stage这些改变,然后commit所有staged的改变,文件又变成unmodified,周而复始,如下图:
使用git status命令
对于使用git init创建的新的项目,使用git status:
1 2 3 4 5 6 | $ git status On branch master Initial commit nothing to commit (create /copy files and use "git add" to track) |
创建一个新的文件,然后再次执行git status:
1 2 3 4 5 6 7 8 9 10 11 12 | $ touch test $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) test nothing added to commit but untracked files present (use "git add" to track) |
提示有untracked files,新创建的test是untracked的。
新创建的文件是untracked,需要手动track到git,使用git add命令:
1 | $ git add test |
此时test被加入到暂存区(staing area),test的状态变成staged,这样下次commit就会提交到仓库,使用git status查看状态:
1 2 3 4 5 6 7 8 9 | $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file : test |
如果git add的参数是目录,将添加目录下的所有文件。
修改test,文件,再次查看其状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ echo a > test $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file : test Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: test |
test的状态变成:Changes not staged for commit,意味着tracked的文件已经改变。为了暂存它,再次使用git add命令:
1 2 3 4 5 6 7 8 9 10 | $ git add test $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file : test |
可以看到,test的状态再次变成staged unmodified。上次的add被覆盖。
注意:使用git add暂存一个文件,是你暂存的那一瞬间的文件内容,就是你后来修改了文件,commit时也是提交暂存时的内容。
总有些文件,我们不想提交到Git仓库,例如编译中间目标文件等。在Git目录下添加.gitignore文件,告诉git忽略该文件指定的那些文件。.gitignore文件的示例:
1 2 3 | $ cat .gitignore *.[oa] *~ |
表示忽略所有的.o和.a文件,以及~结尾的文件,Emacs等使用~标示临时文件。
规则如下:
空行,或者#开头的行被忽略。
标准的glob pattern。
使用/结尾标示目录。
使用 !表示规则的反。
也可以创建一个全局 .gitignore
1 | $ git config --global core.excludesfile ~/.gitignore_global |
使用git diff查看改变,再次改变test,并执行git diff
1 2 3 4 5 6 7 8 9 | $ echo b > test $ git diff diff --git a /test b /test index 7898192..6178079 100644 --- a /test +++ b /test @@ -1 +1 @@ -a +b |
git diff是命令行工具。
还有个命令:git difftool,可以配置外部GUI比较工具,如配置成beyond compare。
1 2 | $ git config --global diff .tool bc3 $ git config --global difftool.bc3.trustExitCode true |
如果想查看暂存区和上一次提交的改变,可以使用 git diff --cached。
1 2 3 4 5 6 7 8 | $ git diff --cached diff --git a /test b /test new file mode 100644 index 0000000..7898192 --- /dev/null +++ b /test @@ -0,0 +1 @@ +a |
提交暂存区中的内容,使用 git commit -m "log"
1 2 3 4 | $ git commit -m "my first commit" [master (root-commit) c4adb19] my first commit 1 file changed, 1 insertion(+) create mode 100644 test |
提交后,暂存区变为空。
上面的提交只会提交暂存区中的内容,如果你想直接提交工作区修改的内容,可以在commit时添加-a选项。
-a选项,会自动暂存修改或删除的文件。
使用 git rm filename
如果文件已经改变,需要加-f选项。
下次commit将从仓库中删除。工作区的文件也被删除。
如果要从暂存区删除
git rm --cached filename
使用:git mv file_from file_to
命令:git log
只会显示log,不会显示改动了那些文件。
git log -p -2
显示详细的改变记录,包括diff的内容,显示2次提交记录。
git log --stat
显示改了那些文件,但不显示改的内容。
GUI工具:gitk
chaning your last commit
git commit --amend
覆盖最后一次提交,但是上次没有交集的内容,还是不被覆盖。
Unstaging a staged file
git reset HEAD <file>
unmodifying a modified file
git checkout -- <file>
查看远端:
git remote -v
1 2 3 | $ git remote - v origin git: //github .com /schacon/grit .git (fetch) origin git: //github .com /schacon/grit .git (push) |
添加远端:
git remote add pb git://github.com/paulboone/ticgit.git
Fetching and Pulling from your remotes
git fetch [remote-name]
git pull
Pushing to your remotes
git push [remote-name] [branch-name]
Inspecting a remote
git remote show [remote-name]
Removing and renaming remotes
git remote rename pb paul
列出tags
1 2 | $ git tag v0.7.0 |
创建Tags
有两种:lightweight 和 annotated
annoated tag
使用 -a创建
1 2 3 | $ git tag -a v1.4 -m 'my version 1.4' $ git tag v1.4 |
使用git show <tag>查看tag
创建lightweight tag
不指定-a -s和-m选项即可 git tag <tagname>
sharing tags
git push origin v1.5