版本表示法: git rev-parse
1.显示分支
$ git rev-parse --symbolic --branches
master
2.显示里程碑
$ git rev-parse --symbolic --tags
hello_1.0
ol_practice
3.显示定义的所有引用
$ git rev-parse --symbolic --glob=refs/*
refs/heads/master
refs/stash
refs/tags/hello_1.0
refs/tags/ol_practice
git rev-parse 另一个重要功能就是将一个git对象表达式表示为对应的SHA1哈希值
1.显示head对应的SHA1哈希值
$ git rev-parse HEAD
bd05a0bdb88cf7eb4c10b084ada2121990561097
$ git describe
hello_1.0-3-gbd05a0b
$ git rev-parse hello_1.0-3-gbd05a0b
bd05a0bdb88cf7eb4c10b084ada2121990561097
$ git rev-parse master refs/heads/master
bd05a0bdb88cf7eb4c10b084ada2121990561097
bd05a0bdb88cf7eb4c10b084ada2121990561097
4.可以用哈希值的前几位指代整个哈希值
$ git rev-parse bd05 bd05a0bd
bd05a0bdb88cf7eb4c10b084ada2121990561097
bd05a0bdb88cf7eb4c10b084ada2121990561097
5.里程碑的两种表示法均指向相同的对象
$ git rev-parse hello_1.0 refs/tags/hello_1.0
8fa1507645a45342db14e9096a11273d4b5d6082
8fa1507645a45342db14e9096a11273d4b5d6082
6.里程碑hello_1.0实际指向的是一个tag对象而非提交
用下面三种表示法可以显示提交本身的哈希值,而非里程碑对象的哈希值
$ git rev-parse hello_1.0^{} hello_1.0^0 hello_1.0^{commit}
06425318860a9bd4f5a0a0e02977282b3af6a16e
06425318860a9bd4f5a0a0e02977282b3af6a16e
06425318860a9bd4f5a0a0e02977282b3af6a16e
版本范围表示法: git rev-list
1.一个提交ID 实际上可以代表一个版本列表,表示该版本开始的所有历史提交
$ git rev-list --oneline hello_1.0
0642531 hello world initiallized
631d7aa README is from welcome.txt
2c5da85 restore file:welcome.txt
4107309 delete trashj file.(using: git add -u)
19d2c13 welcome txt
45ad43e commit '62e5' test
62e5a6a commit in detached HEAD mode
63335a1 does master follow this new commit?
df33bea test1
492478d test
268afa3 test
df69eb9 initialized
2.两个或多个版本,相当于每个版本单独使用时指代的列表的并集
$ git rev-list --oneline hello_1.0 ol_practice
0642531 hello world initiallized
631d7aa README is from welcome.txt
2c5da85 restore file:welcome.txt
4107309 delete trashj file.(using: git add -u)
19d2c13 welcome txt
45ad43e commit '62e5' test
62e5a6a commit in detached HEAD mode
63335a1 does master follow this new commit?
df33bea test1
492478d test
268afa3 test
df69eb9 initialized
3.在一个版本号前面加上符号(^) 表示取反,即排出这个版本及其历史版本
$ git rev-list --oneline ^hello_1.0
浏览日志 git log
1.显示的日志范围git log 后面可以接表示版本范围的参数,当不使用任何表示版本范围的参数时,相当于使用了默认参数HEAD,即显示当前HEAD能够访问到的所有历史提交
$ git log --oneline hello_1.0
0642531 hello world initiallized
631d7aa README is from welcome.txt
2c5da85 restore file:welcome.txt
4107309 delete trashj file.(using: git add -u)
19d2c13 welcome txt
45ad43e commit '62e5' test
62e5a6a commit in detached HEAD mode
63335a1 does master follow this new commit?
df33bea test1
492478d test
268afa3 test
df69eb9 initialized
2.分支图显示
$ git log --oneline --graph hello_1.0
* 0642531 hello world initiallized
* 631d7aa README is from welcome.txt
* 2c5da85 restore file:welcome.txt
* 4107309 delete trashj file.(using: git add -u)
* 19d2c13 welcome txt
* 45ad43e commit '62e5' test
|\
| * 62e5a6a commit in detached HEAD mode
* | 63335a1 does master follow this new commit?
|/
* df33bea test1
* 492478d test
* 268afa3 test
* df69eb9 initialized
3.显示最近的几条日志
$ git log --oneline -3 hello_1.0
0642531 hello world initiallized
631d7aa README is from welcome.txt
2c5da85 restore file:welcome.txt
4.显示每次提交的具体改动
$ git log -p
commit bd05a0bdb88cf7eb4c10b084ada2121990561097
Author: yinnana <nanayin@creditease.cn>
Date: Sun Jan 8 12:23:22 2017 +0800
doc commit
diff --git "a/doc/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/doc/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"
new file mode 100644
index 0000000..e69de29
commit fece0beb413478122dbe4e2dcbb38469d51cee55
Author: yinnana <nanayin@creditease.cn>
Date: Sun Jan 8 11:42:26 2017 +0800
偷懒了,直接使用-a参数直接提交
diff --git a/src/yin.do b/src/yin.do
index 753ff4f..657ad52 100644
--- a/src/yin.do
+++ b/src/yin.do
@@ -1 +1,2 @@
5.显示每次提交的变更概要
使用 -p 参数会让日志输出变得非常冗余,当不需要知道具体的改动而只想知道改动在哪些文件上,可以使用 --stat 参数
$ git log --stat --oneline
bd05a0b doc commit
...6\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" | 0
1 file changed, 0 insertions(+), 0 deletions(-)
fece0be 偷懒了,直接使用-a参数直接提交
src/yin.do | 1 +
1 file changed, 1 insertion(+)
a1158c1 add yin.do
src/yin.do | 1 +
1 file changed, 1 insertion(+)
0642531 hello world initiallized
src/main.c | 9 +++++++++
src/version.h.in | 5 +++++
2 files changed, 14 insertions(+)
6.定制输出
--pretty=raw 显示commit的原始数据,可以显示提交对应的树ID
$ git log --pretty=raw
commit bd05a0bdb88cf7eb4c10b084ada2121990561097
tree db514052bc842620c70ec5891da26452745d2186
parent fece0beb413478122dbe4e2dcbb38469d51cee55
author yinnana <nanayin@creditease.cn> 1483849402 +0800
committer yinnana <nanayin@creditease.cn> 1483849402 +0800
doc commit
commit fece0beb413478122dbe4e2dcbb38469d51cee55
tree 4c79be14a76b90abf5f99790565153dd0c377413
parent a1158c1237d9baa1a72bec50219ff14c95fcb6e2
author yinnana <nanayin@creditease.cn> 1483846946 +0800
committer yinnana <nanayin@creditease.cn> 1483846946 +0800
偷懒了,直接使用-a参数直接提交
参数--pretty=fuller会同时显示作者和提交者
$ git log --pretty=fuller
commit bd05a0bdb88cf7eb4c10b084ada2121990561097
Author: yinnana <nanayin@creditease.cn>
AuthorDate: Sun Jan 8 12:23:22 2017 +0800
Commit: yinnana <nanayin@creditease.cn>
CommitDate: Sun Jan 8 12:23:22 2017 +0800
doc commit
commit fece0beb413478122dbe4e2dcbb38469d51cee55
Author: yinnana <nanayin@creditease.cn>
AuthorDate: Sun Jan 8 11:42:26 2017 +0800
Commit: yinnana <nanayin@creditease.cn>
CommitDate: Sun Jan 8 11:42:26 2017 +0800
偷懒了,直接使用-a参数直接提交
参数--pretty=oneline 会提供最精简的日志输出,也可以使用 --oneline参数
$ git log --pretty=oneline
bd05a0bdb88cf7eb4c10b084ada2121990561097 doc commit
fece0beb413478122dbe4e2dcbb38469d51cee55 偷懒了,直接使用-a参数直接提交
a1158c1237d9baa1a72bec50219ff14c95fcb6e2 add yin.do
06425318860a9bd4f5a0a0e02977282b3af6a16e hello world initiallized
631d7aa16079dee4c77acd237ce0775071026cc2 README is from welcome.txt
2c5da855ffec0ae6d67cbf6812fd8278f3e6fc1d restore file:welcome.txt
4107309c36f552500f479c85b5cd313d114589d8 delete trashj file.(using: git add -u)
19d2c13bd1eaa3e1c8319142fd6daaf6d0f29c52 welcome txt
45ad43e40b1c76bc08f36d1d31ab08562b54652d commit '62e5' test
62e5a6a9ad9cdbcc5907068a2c3d1db64a1bb824 commit in detached HEAD mode
63335a198552e72e7f4a59a265769c96df0e57d3 does master follow this new commit?
df33bea3141db42c47dfb8fc2ce1d1d9416100e2 test1
492478d7ebc1e6bc7104e585c6fe2b38185521c3 test
268afa36b9b348228fd2fc77b12423d9e97d710b test
df69eb991294825b467ca423d38aedd4117ca0f1 initialized
如果只是查看和分析某一个提交,可以使用git show 或 git cat-file命令
使用git show 显示里程碑df69eb991294825b467ca423d38aedd4117ca0f1及其提交
$ git show df69eb991294825b467ca423d38aedd4117ca0f1 --stat
commit df69eb991294825b467ca423d38aedd4117ca0f1
Author: yinnana <nanayin@creditease.cn>
Date: Wed Jan 4 12:35:39 2017 +0800
initialized
welcome.txt | 1 +
1 file changed, 1 insertion(+)
使用git cat-file 显示里程碑df69eb991294825b467ca423d38aedd4117ca0f1及其提交,参数-p的含义是美观的输出(pretty)
$ git cat-file -p df69eb991294825b467ca423d38aedd4117ca0f1
tree 2f440402864b331c9f4e75e0cb21f513d12cac45
author yinnana <nanayin@creditease.cn> 1483504539 +0800
committer yinnana <nanayin@creditease.cn> 1483504539 +0800
initialized
差异比较:git diff
1.比较里程碑B和里程碑A git diff B A
2.比较工作区和里程碑A git diff A
3.比较暂存区和里程碑A git diff --cached A
4.比较工作区和暂存区 git diff
5.比较暂存区和HEAD git diff --cached
6.比较工作区和HEAD git diff HEAD
1)文件不同版本的差异比较
差异比较还可以使用路径参数,只显示不同版本间改路径下文件的差异
$ git diff df69eb991294825b467ca423d38aedd4117ca0f1 bd05a0bdb88cf7eb4c10b084ada2121990561097 -- README
diff --git a/README b/README
new file mode 100644
index 0000000..800e539
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+bye-bye
2)非git目录/文件的差异比较
git diff 还可以在git版本库之外执行,对非git目录进行比较,
git提供了对二进制文件差异等的扩展支持
$ git diff f:/11.txt f:/22.txt
diff --git a/f:/11.txt b/f:/22.txt
index c473a1d..f3736ef 100644
--- a/f:/11.txt
+++ b/f:/22.txt
@@ -1,2 +1 @@
-qq
-dd
\ No newline at end of file
+qq
\ No newline at end of file
git扩展了GNU的差异比较语法,提供了对重命名、二进制文件、文件权限变更的支持
4)逐词比较,而非默认的逐行比较
git的差异比较默认是逐行比较,分别显示改动前和改动后的行,到底改动在哪里还需要仔细辨别
参数--word-diff 可以显示逐词比较
$ git diff --word-diff f:/11.txt f:/22.txt
diff --git a/f:/11.txt b/f:/22.txt
index c473a1d..f3736ef 100644
--- a/f:/11.txt
+++ b/f:/22.txt
@@ -1,2 +1 @@
qq[-dd-]
文件追溯: git blame
$ git blame ElasticScreenAction.java
6e778a40 (taoye 2016-10-24 19:53:53 +0800 1) package corp.credit.telephon e.action;
6e778a40 (taoye 2016-10-24 19:53:53 +0800 2)
d170bf94 (taoye 2017-01-06 17:08:13 +0800 3) import corp.credit.Case.bean .RmCasePushVO;
b745f3f1 (taoye 2016-10-27 19:34:58 +0800 4) import corp.credit.base.comm ents.Query;
b745f3f1 (taoye 2016-10-27 19:34:58 +0800 5) import corp.credit.base.comm ents.QueryImpl;
9c8e4da7 (taoye 2016-11-10 13:53:49 +0800 6) import corp.credit.base.conf ig.InitData;
83e07691 (taoye 2016-11-18 13:16:33 +0800 7) import corp.credit.base.util .*;
458fe547 (taoye 2016-11-23 18:32:23 +0800 8) import corp.credit.customern ew.bean.CsCustomer;
6e778a40 (taoye 2016-10-24 19:53:53 +0800 9) import corp.credit.system.ac tion.BaseAction;
9c8e4da7 (taoye 2016-11-10 13:53:49 +0800 10) import corp.credit.system.be
只想查看某几行,使用-L,N,M参数
$ git blame -L 6,+5 ElasticScreenAction.java
9c8e4da7 (taoye 2016-11-10 13:53:49 +0800 6) import corp.credit.base.config.Ini tData;
83e07691 (taoye 2016-11-18 13:16:33 +0800 7) import corp.credit.base.util.*;
458fe547 (taoye 2016-11-23 18:32:23 +0800 8) import corp.credit.customernew.bea n.CsCustomer;
6e778a40 (taoye 2016-10-24 19:53:53 +0800 9) import corp.credit.system.action.B aseAction;
9c8e4da7 (taoye 2016-11-10 13:53:49 +0800 10) import corp.credit.system.bean.CsU ser;
二分查找:git bisect