本文将介绍下列命令的常见用法:
git log
git reflog
1. git log
git log [[--] path...]是非常有用的一个命令,可以用来查看提交历史。如果运行此命令时不指定path,命令会列出当前仓库全部的历史日志,按提交的时间倒叙排列。Path可以为任意的目录路径或者具体的文件名。可以指定多个path,path之间用空格间隔。
下面是在gitsample仓库运行的例子(--pretty=oneline参数在许多git命令中都可以使用,作用是将每次操作的信息都在一行显示):
$ git log --pretty=oneline
4a567b974a67bcee298cbbeebe15db5731db2991 (HEAD -> master, origin/master, origin/HEAD) 2nd time modified.
2dc9fe4dee3d416749c062cb7384b6606bc34bc0 init sample
4786186937ae3402e92b8f3023a9c65e97ef5580 Initial commit
可以看到不指定path参数的git log列出了全部的3次提交。接下来再看指定path参数的效果:
$ git log workspace/index.php
commit 2dc9fe4dee3d416749c062cb7384b6606bc34bc0
Author: Efrey Kong <efreykong@outlook.com>
Date: Fri Aug 16 17:34:57 2019 +0800
init sample
这次只显示了包含指定文件的提交。
$ git log workspace
commit 2dc9fe4dee3d416749c062cb7384b6606bc34bc0
Author: Efrey Kong <efreykong@outlook.com>
Date: Fri Aug 16 17:34:57 2019 +0800
init sample
另外还可以通过增加命令参数来查看特定的日志结果,常用的参数有:
1). -2:只查看最近两次的提交。可以按自己需要来修改数字,例如-3是查看最近3次提交。
2). -p:查看提交的差异对比
3). --stat:查看提交文件变化的简要统计
4). --pretty=oneline:限制提交的简要信息在一行内显示
5). --since=<时间条件>:根据时间条件来筛选提交。例如--since=2.weeks筛选最近两周的提交;又如--since=2.years筛选最近两年的提交;再如--since=2014-1-10筛选2014年1月10日的提交等。
6). --reflog:像reflog命令那样,将所有的commit操作都列出来。例如,仓库的最后一次提交操作被回撤了,在用git log查看日志时,如果不加--reflog参数是不会显示被回撤的提交操作的。
接下来还是使用gitsample仓库作为示例依次演示命令以及参数的执行
$ git log
commit 4a567b974a67bcee298cbbeebe15db5731db2991 (HEAD -> master, origin/master, origin/HEAD)
Author: Efrey Kong <efreykong@outlook.com>
Date: Fri Aug 16 17:36:48 2019 +0800
2nd time modified.
commit 2dc9fe4dee3d416749c062cb7384b6606bc34bc0
Author: Efrey Kong <efreykong@outlook.com>
Date: Fri Aug 16 17:34:57 2019 +0800
init sample
commit 4786186937ae3402e92b8f3023a9c65e97ef5580
Author: efrey kong <efreykong@outlook.com>
Date: Fri Aug 16 17:28:54 2019 +0800
Initial commit
从上面的结果可以看到目前仓库有3次提交,命令显示了提交的简要信息:每次提交的ID(例如4786186937ae3402e92b8f3023a9c65e97ef5580)、作者、日期信息,还有提交的简要说明。
$ git log -p -1
commit 4a567b974a67bcee298cbbeebe15db5731db2991 (HEAD -> master, origin/master, origin/HEAD)
Author: Efrey Kong <efreykong@outlook.com>
Date: Fri Aug 16 17:36:48 2019 +0800
2nd time modified.
diff --git a/README.md b/README.md
index f3d29fb..8d42e6b 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
# gitsample
-This line is the first time modified.
\ No newline at end of file
+This line is the first time modified.
+
+This line is the second time modified.
\ No newline at end of file
$ git log --stat -1 --pretty=oneline
4a567b974a67bcee298cbbeebe15db5731db2991 (HEAD -> master, origin/master, origin/HEAD) 2nd time modified.
README.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
2. git reflog
显示本地git仓库操作的参考日志。git log和git reflog从字面上有点像,常常引起初学者的困惑。这里简单的讲一下两者的区别:
git log可以查看Git仓库所有的提交的历史日志。这些提交在和远程Git仓库同步数据的时候会一起被更新的。当你从远程仓库克隆了本地仓库后,你在本地仓库可以看到其他人的提交。
git reflog可以查看发生在本地的所有操作。举个例子,有时候,开发者在本地进行过多次提交,后来又撤回了某一个或几个提交。随后将提交同步到远程仓库:只有有效的提交会被同步,而被撤销的提交属于无效的提交不会被同步。这个被撤销的提交只能在这个本地仓库用reflog命令查看,用log命令是查看不到的。
在gitsample项目位置运行reflog:
$ git reflog
4a567b9 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: clone: from https://github.com/efreykongcn/gitsample
reflog命令结果只有一条,也就是本地的clone操作。gitsample仓库共有3次提交,但是这些提交操作并不是当前这个仓库发生的,所以reflog中不会显示。而log命令可以查看到全部的3条提交。