git log

本文于2017年12月29号发布在个人博客中,因为个人博客关闭,全部迁移到CSDN,以下是正文:


想知道都有哪些提交吗?“git log”可以帮你实现啊

进入任何仓库目录,输入命令:

$ git log
commit e9cec2032aadc0f630530ac1dd63f2c92c70d1dc
Author: anyscoding .com>
Date: Sun Dec 10 08:21:56 2017 +0800

push code to remote

1. add a new line to README.md
2. add a new file

commit 4a3f515b52ac34d421d52b8fe13918a63b94ee62
Author: cug_heshun .com>
Date: Fri Dec 8 07:06:58 2017 +0800

undo commit

默认情况下,git log会按照时间逆序(提交越晚显示越靠前)分页显示所有的提交记录,每条提交记录包括:

  • SHA-1 commit id
  • 作者,用户名和邮箱
  • 时间
  • commit message

git log有很多参数,本文只列出几个常用的,如下表:

选项 说明
-n 仅显示最近的 n 条提交
-p 按补丁格式显示每个更新之间的差异
–state 显示每次更新的文件修改统计信息
–pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)
–graph 显示 ASCII 图形表示的分支合并历史
–abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符
–since, –after 仅显示指定时间之后的提交
–until , –before 仅显示指定时间之前的提交
–author 仅显示指定作者相关的提交
–committer 仅显示指定提交者相关的提交
–grep 仅显示含指定关键字的提交
-S 仅显示添加或移除了某个关键字的提交

简单的参数,诸如:

-n, -p, --state, --graph, --abbrev-commit, --author, --committer等

就不做详细的介绍了,下面讲一讲几个稍微复杂点的参数

为了方便演示,接下来的示例都会带上“–abbrev-commit”(仅显示 SHA-1 的前几个字符,而非所有的 40 个字符)参数

–pretty

–pretty命令的选项又有多个:

oneline,把提交记录显示在一行上,显示的内容包括:
    SHA-1 "commit message title"
short,提交记录的简短介绍,显示的内容包括:
    commit SHA-1前几个字符
    Author: xxx
    commit message title
full,相比short多了Committer
fuller,相比full多了author date和commit date
format,后跟格式控制字符,命令为:git log --pretty=format:"xx xx",格式控制字符见下表
选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 –date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明

来看一个format的示例:

$ git log --abbrev-commit --pretty=format:"%h %an %cn %s"
4cb5d87 anyscoding anyscoding first commit to develop branch
a3bdf3b anyscoding GitHub add
3a0b882 anyscoding GitHub delete lines for aws test
e386e10 anyscoding GitHub add new line for aws test again
d132579 anyscoding GitHub add new line for aws test build action output
77ccea1 anyscoding GitHub delete added lines in README.md
541efff anyscoding GitHub add new line for aws
cce0567 anyscoding GitHub Update README.md
e9cec20 anyscoding anyscoding push code to remote
4a3f515 cug_heshun cug_heshun undo commit
28ca9cc cug_heshun cug_heshun just test
9b4e410 anyscoding GitHub Initial commit

–since/–after和–until/–before

版本转测在即,项目owner需要整理当前版本新增特性以及修复bug的列表,查询指定时间内的历史记录就可以排上用场了

限制起始时间:–after, –since。–after/–since=”2017-12-07″表示2017-12-07之后,不包括2017-12-07

限制结束时间:–before, –until。–before/–until=”2017-12-11″表示2017-12-11之前,不包括2017-12-11

$ git log --pretty=format:"%h %cn %cd %s" --since="2017-12-07" --until="2017-12-11"
e9cec20 anyscoding Sun Dec 10 08:21:56 2017 +0800 push code to remote
4a3f515 cug_heshun Fri Dec 8 07:06:58 2017 +0800 undo commit
28ca9cc cug_heshun Fri Dec 8 07:02:42 2017 +0800 just test
9b4e410 GitHub Fri Dec 8 06:48:00 2017 +0800 Initial commit

$ git log --pretty=format:"%h %cn %cd %s" --after="2017-12-07" --before="2017-12-11"
e9cec20 anyscoding Sun Dec 10 08:21:56 2017 +0800 push code to remote
4a3f515 cug_heshun Fri Dec 8 07:06:58 2017 +0800 undo commit
28ca9cc cug_heshun Fri Dec 8 07:02:42 2017 +0800 just test
9b4e410 GitHub Fri Dec 8 06:48:00 2017 +0800 Initial commit

an@DESKTOP-IEU7HQD MINGW64 /d/GitHub/HelloWorld (master)
$

–grep

有些时候,可能是回归测试,也可能是为了给其他程序员演示某些特性的写法,需要查找commit message中包含了特定字符的提交,–grep就可以排上用场了

$ git log --grep="aws" --pretty=oneline
3a0b882ff42121f72f2490d9c1eced342cbcdef0 delete lines for aws test
e386e10bac03d41c2fc697d3148ed0b8a5cbe906 add new line for aws test again
d132579b93ce58c187a3e8d249400063b80ad97f add new line for aws test build action output
541efff5c876b971ce74174eb553bb10c484d42c add new line for aws

$

-S

想象一下,你的代码现在跑不起来了,错误信息提示找不到某标识符,肯定是被谁删掉了,我们需要找出来这次提交记录,做一番详细的审查,-S命令就可以派上用场了

$ git log -S"first"
commit 9b4e410354a3c48bfc8993b4d096f24b89df828f
Author: anyscoding <34288556+anyscoding@users.noreply.github.com>
Date: Fri Dec 8 06:48:00 2017 +0800

Initial commit

$ git log -S"develop"
commit 4cb5d8773ac2867b813901dec98c7c072c59e4d0 (HEAD -> master)
Author: anyscoding .com>
Date: Wed Dec 27 07:40:17 2017 +0800

first commit to develop branch

$

git show

如上,找到删除或修改的提交记录之后,还需要看个究竟,可以使用git show命令查看指定commit的修改内容

$ git show 4cb5d8773ac2867b813901dec98c7c072c59e4d0
commit 4cb5d8773ac2867b813901dec98c7c072c59e4d0 (HEAD -> master)
Author: anyscoding 
Date: Wed Dec 27 07:40:17 2017 +0800

first commit to develop branch

diff --git a/note.md b/note.md
new file mode 100644
index 0000000..c9764c1
--- /dev/null
+++ b/note.md
@@ -0,0 +1 @@
+this is a develop branch

$

特定路径的历史记录

git log /path/to/file 命令可以查看指定路径的历史记录

$ git log --pretty=oneline README.md
a3bdf3b8a1b2332597ebd0417dab42a6a6b2eb5a (origin/master, origin/HEAD) add
3a0b882ff42121f72f2490d9c1eced342cbcdef0 delete lines for aws test
e386e10bac03d41c2fc697d3148ed0b8a5cbe906 add new line for aws test again
d132579b93ce58c187a3e8d249400063b80ad97f add new line for aws test build action output
77ccea126fdc3ff2ab3a92d5e9458af457090812 delete added lines in README.md
541efff5c876b971ce74174eb553bb10c484d42c add new line for aws
cce0567986c702792a28ff510fef6c585e68344f Update README.md
e9cec2032aadc0f630530ac1dd63f2c92c70d1dc push code to remote
9b4e410354a3c48bfc8993b4d096f24b89df828f Initial commit

你可能感兴趣的:(git)