git 常用命令说明 (二)

in git with 0 comment

1.

git status

//查看工作区的状态

$ git status
# On branch master
# 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: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

git diff

//查看修改的内容

$ git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

2.

git reset --hard "commit_id"

//版本回退,通过各个版本状态的提交码的前面七位字符就可以

$ git reset --hard 3628164
HEAD is now at 3628164 append GPL

git log

//可以查看提交历史,以便确定要回退到哪个版本。

$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

git reflog

//要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file

3.

git checkout -- file

//当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时

$ git checkout -- readme.txt

$ git status
# On branch master
nothing to commit (working directory clean)

git reset HEAD file

//当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步使用此命令,回到工作区,第二步用如上的命令,丢弃工作区中的修改。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   readme.txt
#

$ git reset HEAD readme.txt
Unstaged changes after reset:
M       readme.txt

$ git checkout -- readme.txt

$ git status
# On branch master
nothing to commit (working directory clean)

4.

git rm

//用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

$ git checkout -- test.txt

5.

查看分支:git branch

创建分支:git branch <name>

切换分支:git checkout <name>

创建+切换分支:git checkout -b <name>

合并某分支到当前分支:git merge <name>

删除分支:git branch -d <name>

$ git checkout -b newtest
Switched to a new branch 'newtest'
等于如下命令
$ git branch newtest
$ git checkout newtest
Switched to branch 'newtest'
$ git branch
* newtest
 master

//git branch命令会列出所有分支,当前分支前面会标一个*号。

$ git checkout master
Switched to branch 'master'

//切换回master分支

$ git merge newtest
Updating d17efd8..fec145a
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

//git merge命令用于合并指定分支到当前分支。合并后,再查看readme.txt的内容,就可以看到,和newtest分支的最新提交是完全一样的。

注意到上面的Fast-forward信息,Git告诉我们,这次合并是“快进模式”,也就是直接把master指向dev的当前提交,所以合并速度非常快。

当然,也不是每次合并都能Fast-forward,我们后面会讲其他方式的合并。

$ git merge --no-ff -m "merge with no-ff" newtest
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

//合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

$ git branch -d newtest
Deleted branch newtest (was fec145a).

$ git branch
* master

//合并完成后,可以删除分支。

git log --graph

//查看分支合并图

$ git log --graph --pretty=oneline --abbrev-commit
* 59bc1cb conflict fixed
|\
| * 75a857c AND simple
* | 400b400 & simple
|/
* fec145a branch test
...

git stash

git stash list

$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge

//stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作

$ git stash list
stash@{0}: WIP on dev: 6224937 add merge

//工作区是干净的,刚才的工作现场存到哪去了?用git stash list命令看看:工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:

$ git stash pop
# On branch dev
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: hello.py
#
# 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: readme.txt
#
Dropped refs/stash@{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)
$ git stash list              //此时看不到了
$ git stash apply stash@{0}           //恢复指定的stash

//修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。

git branch -D <name>

//如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除。

$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 756d4af).

6.

git tag <name>

//创建标签

$ git branch
* dev
 master
$ git checkout master //首先,切换到需要打标签的分支上:
Switched to branch 'master'

$ git tag v1.0 //创建新标签

$ git tag //查看所有标签
v1.0
//默认标签是打到最新的一次提交上,如果忘了打标签,则可以找到历史提交的commit id,然后打上就可以了
$ git log --pretty=oneline --abbrev-commit
6a5819e merged bug fix 101
cc17032 fix bug 101
7825a50 merge with no-ff
6224937 add merge
59bc1cb conflict fixed
400b400 & simple
75a857c AND simple
fec145a branch test
d17efd8 remove test.txt

$ git tag v0.9 6224937 //将add merge的提交标签为v0.9
$ git tag //查看标签
v0.9
v1.0

git show <tagname>

//查看标签详细信息

$ git show v0.9
commit 622493706ab447b6bb37e4e2a2f276a20fed2ab4
Author: Michael Liao <askxuefeng@gmail.com>
Date: Thu Aug 22 11:22:08 2013 +0800

 add merge
...

git tag -a <tagname> -m "blablabla..."可以指定标签信息;

git tag -s <tagname> -m "blablabla..."可以用PGP签名标签;

文章参考:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

Responses