菜鸟笔记
提升您的技术认知

git 查看提交历史-ag真人游戏

阅读 : 1021

在日常开发中我们每天都在提交自己的更新代码之仓库,那么作为管理人员或者自己如何来查看提交了哪些呢?

使用 git log 命令列出历史提交记录如下:

看到下面的输出:

$ git log
commit 88afe0e02adcdfea6844bb627de97da21eb10af1
merge: 14b4dca d7e7346
author: w3cschool 
date:   sun mar 1 15:03:42 2015  0800
    merge branch 'change_site'
    conflicts:
        test.txt
commit 14b4dcadbdc847207651d5a9fae0d315057f346e
author: w3cschool 
date:   sun mar 1 14:53:15 2015  0800
    新增加一行
commit d7e734640da06055e107eaf29cf350b3f1de1c2c
author: w3cschool 
date:   sun mar 1 14:48:57 2015  0800
    changed the site
commit 556f0a0637978097b82287ac665a717623b21f3f
author: w3cschool 
date:   sun mar 1 14:40:34 2015  0800
    removed test2.txt
 $ git log
commit 88afe0e02adcdfea6844bb627de97da21eb10af1
merge: 14b4dca d7e7346
author: w3cschool 
date:   sun mar 1 15:03:42 2015  0800
    merge branch 'change_site'
    conflicts:
        test.txt
commit 14b4dcadbdc847207651d5a9fae0d315057f346e
author: w3cschool 
date:   sun mar 1 14:53:15 2015  0800
    新增加一行
commit d7e734640da06055e107eaf29cf350b3f1de1c2c
author: w3cschool 
date:   sun mar 1 14:48:57 2015  0800
    changed the site
commit 556f0a0637978097b82287ac665a717623b21f3f
author: w3cschool 
date:   sun mar 1 14:40:34 2015  0800
    removed test2.txt

默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面。看到了吗,每次更新都有一个 sha-1 校验和、作者的名字和电子邮件地址、提交时间,最后缩进一个段落显示提交说明。

我们可以用 --oneline 选项来查看历史记录的简洁的版本。

$ git log --oneline
88afe0e merge branch 'change_site'
14b4dca 新增加一行
d7e7346 changed the site
556f0a0 removed test2.txt
2e082b7 add test2.txt
048598f add test.txt
85fc7e7 test comment from coonote.com

这告诉我们的是,此项目的开发历史。

我们还可以用 --graph 选项,查看历史中什么时候出现了分支、合并。以下为相同的命令,开启了拓扑图选项:

$ git log --oneline --graph
\*   88afe0e merge branch 'change_site'
| * d7e7346 changed the site
\* | 14b4dca 新增加一行
\* 556f0a0 removed test2.txt
\* 2e082b7 add test2.txt
\* 048598f add test.txt
\* 85fc7e7 test comment from coonote.com

现在我们可以更清楚明了地看到何时工作分叉、又何时归并。
你也可以用 '--reverse'参数来逆向显示所有日志。

$ git log --reverse --oneline
85fc7e7 test comment from coonote.com
048598f add test.txt
2e082b7 add test2.txt
556f0a0 removed test2.txt
d7e7346 changed the site
14b4dca 新增加一行
88afe0e merge branch 'change_site'

如果只想查找指定用户的提交日志可以使用命令:git log --author , 例如,比方说我们要找 git 源码中 linus 提交的部分:

$ git log --author=linus --oneline -5
81b50f3 move 'builtin-*' into a 'builtin/' subdirectory
3bb7256 make "index-pack" a built-in
377d027 make "git pack-redundant" a built-in
b532581 make "git unpack-file" a built-in
112dd51 make "mktag" a built-in

如果你要指定日期,可以执行几个选项:--since 和 --before,但是你也可以用 --until 和 --after。
例如,如果我要看 git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了 --no-merges 选项以隐藏合并提交):

$ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
5469e2d git 1.7.1-rc2
d43427d documentation/remote-helpers: fix typos and improve language
272a36b fixup: second argument may be any arbitrary string
b6c8d2d documentation/remote-helpers: add invocation section
5ce4f4e documentation/urls: rewrite to accomodate transport::address
00b84e9 documentation/remote-helpers: rewrite description
03aa87e documentation: describe other situations where -z affects git diff
77bc694 rebase-interactive: silence warning when no commits rewritten
636db2c t3301: add tests to use --format="%n"

git log 有许多选项可以帮助你搜寻感兴趣的提交
选项说明-p

按补丁格式显示每个更新之间的差异。

--word-diff

按 word diff 格式显示差异。

--stat

显示每次更新的文件修改统计信息。

--shortstat

只显示 --stat 中最后的行数修改添加移除统计。

--name-only

仅在提交信息后显示已修改的文件清单。

--name-status

显示新增、修改、删除的文件清单。

--abbrev-commit

仅显示 sha-1 的前几个字符,而非所有的 40 个字符。

--relative-date

使用较短的相对时间显示(比如,“2 weeks ago”)。

--graph

显示 ascii 图形表示的分支合并历史。

--pretty

使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。

--oneline

--pretty=oneline --abbrev-commit 的简化用法。

限制输出长度

除了定制输出格式的选项之外,git log 还有许多非常实用的限制输出长度的选项,也就是只输出部分提交信息。之前我们已经看到过 -2 了,它只显示最近的两条提交,实际上,这是 - 选项的写法,其中的 n 可以是任何自然数,表示仅显示最近的若干条提交。不过实践中我们是不太用这个选项的,git 在输出所有提交时会自动调用分页程序(less),要看更早的更新只需翻到下页即可。

另外还有按照时间作限制的选项,比如 --since 和 --until。下面的命令列出所有最近两周内的提交:

$ git log --since=2.weeks
你可以给出各种时间格式,比如说具体的某一天(“2008-01-15”),或者是多久以前(“2 years 1 day 3 minutes ago”)。

还可以给出若干搜索条件,列出符合的提交。用 --author 选项显示指定作者的提交,用 --grep 选项搜索提交说明中的关键字。(请注意,如果要得到同时满足这两个选项搜索条件的提交,就必须用 --all-match 选项。否则,满足任意一个条件的提交都会被匹配出来)

另一个真正实用的git log选项是路径(path),如果只关心某些文件或者目录的历史提交,可以在 git log 选项的最后指定它们的路径。因为是放在最后位置上的选项,所以用两个短划线(--)隔开之前的选项和后面限定的路径名。

表 2-3 还列出了其他常用的类似选项。

选项说明-(n)

仅显示最近的 n 条提交

--since, --after

仅显示指定时间之后的提交。

--until, --before

仅显示指定时间之前的提交。

--author

仅显示指定作者相关的提交。

--committer

仅显示指定提交者相关的提交。

网站地图