git log

本文详细介绍了git log的常用参数,包括--pretty定制输出格式,--since/--after和--until/--before筛选时间范围,--grep搜索特定commit message,-S查找代码变动,以及git show查看提交详情。通过实例演示了如何利用这些参数来高效地查阅git历史记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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

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

$ git log
commit e9cec2032aadc0f630530ac1dd63f2c92c70d1dc
Author: anyscoding <anyscoding@sina.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 <cug_heshun@sina.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 <anyscoding@sina.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 <anyscoding@sina.com>
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
<think>好的,用户让我介绍一下Git log。首先,我需要确定用户对Git的基础知识了解多少。可能用户已经知道基本的版本控制概念,但需要更深入了解如何查看提交历史。 接下来,我应该回顾Git log的基本功能,它用于查看项目的提交历史。用户可能想知道常用的选项和参数,比如如何简化输出、查看差异,或者按时间、作者过滤提交记录。需要分步骤介绍,让用户能够逐步掌握。 然后,考虑用户可能遇到的常见问题,比如日志太多难以阅读,或者需要查找特定提交。这时候,介绍一些常用的选项,比如--oneline、--graph、--since等,会有帮助。同时,示例很重要,用户可以通过实际的命令和输出来更好地理解。 还要注意格式问题,用户提到的系统级指令中,要求数学表达式用$...$,但这里可能不太涉及数学内容,所以可能不需要用到。不过要确保中文回答,结构清晰,步骤明确。 另外,用户可能需要知道如何自定义输出格式,比如使用--pretty=format来自定义显示的提交信息,这对定制化需求很有用。还有,如何查看文件的修改历史,比如使用git log <file>,这也是常见的使用场景。 需要检查是否有遗漏的重要选项,比如查看分支合并情况的--graph,或者如何限制显示的提交数量,比如-2。同时,提醒用户可以通过组合不同的选项来达到更精确的查找,比如同时使用--since和--author。 最后,确保语言简洁明了,避免技术术语过多,让不同水平的用户都能理解。可能需要总结常用命令,方便用户快速查阅。还要确认所有命令示例正确,符合LaTeX格式要求,但在这个上下文中可能不需要数学表达式,所以主要关注代码块的正确格式。</think>Git logGit 版本控制系统中用于查看提交历史的命令。通过逐步掌握以下内容,你可以有效追踪项目变更: 一、基础用法 1. 输入 `git log` 会显示完整提交历史 ```bash commit 3a7b8c1d (HEAD -> main) Author: 张三 <zhangsan@example.com> Date: Mon Jan 1 12:00:00 2023 +0800 添加用户登录功能 commit 5d2f4e9a Author: 李四 <lisi@example.com> Date: Sun Dec 31 09:30:00 2022 +0800 初始化项目结构 ``` 二、常用参数(组合使用效果更佳) 1. 精简显示:`git log --oneline` $$3a7b8c1 (HEAD -> main) 添加用户登录功能$$ $$5d2f4e9 初始化项目结构$$ 2. 图形化分支:`git log --graph --decorate` ```bash * commit 3a7b8c1 (main) | Author: 张三 | 添加登录功能 | * commit 5d2f4e9 初始化项目 ``` 3. 过滤提交 - 时间范围:`git log --since="2023-01-01" --until="2023-01-31"` - 按作者:`git log --author="张三"` - 按关键词:`git log -S "login"`(搜索包含"login"的修改) 三、高级应用 1. 查看文件历史:`git log README.md`(追踪特定文件变更) 2. 显示修改内容:`git log -p`(附带代码差异对比) 3. 自定义格式: ```bash git log --pretty=format:"%h | %an | %ad | %s" ``` 输出示例: $$3a7b8c1 | 张三 | Mon Jan 1 12:00:00 2023 +0800 | 添加用户登录功能$$ 四、实用技巧 1. 限制显示数量:`git log -2`(仅显示最新2条记录) 2. 时间线筛选:`git log --after="2 weeks ago"` 3. 组合命令:`git log --oneline --graph --all`(查看全部分支关系) 建议通过 `git help log` 查看完整文档。实际使用时,建议将常用参数设为别名: ```bash git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'" ``` 之后通过 `git lg` 即可获得彩色可视化日志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值