progit2 Pro Git 2nd Edition 项目地址: https://gitcode.com/gh_mirrors/pr/progit2
在版本控制系统中,查看提交历史是最基础也是最重要的操作之一。Git提供了强大的git log
命令来满足这一需求。当你在项目目录中直接运行git log
时,会看到类似如下的输出:
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon
Date: Mon Mar 17 21:52:11 2008 -0700
Change version number
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon
Date: Sat Mar 15 16:40:33 2008 -0700
Remove unnecessary test
默认情况下,git log
会按照时间倒序列出所有提交记录,每个提交包含以下关键信息:
使用-p
或--patch
选项可以显示每次提交引入的具体变更内容:
$ git log -p -1
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon
Date: Mon Mar 17 21:52:11 2008 -0700
Change version number
diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = "simplegit"
- s.version = "0.1.0"
+ s.version = "0.1.1"
这个功能特别适合代码审查或快速了解某次提交的具体变更内容。
--stat
选项提供了更简洁的变更概览:
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon
Date: Mon Mar 17 21:52:11 2008 -0700
Change version number
Rakefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
输出中会显示:
--pretty
选项提供了多种预设格式:
oneline:单行简洁显示
$ git log --pretty=oneline
ca82a6d Change version number
085bb3b Remove unnecessary test
format:完全自定义格式
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : Change version number
常用格式占位符包括:
%H
:完整提交哈希%h
:简短提交哈希%an
:作者姓名%ar
:相对作者日期%s
:提交信息主题结合--graph
选项可以显示分支合并的ASCII图形:
$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 Ignore errors from SIGCHLD on trap
* 5e3ee11 Merge branch 'master'
|\
| * 420eac9 Add method for getting the current branch
使用-n
查看最近的n条记录:
$ git log -2 # 查看最近2条提交
$ git log --since="2023-01-01" --until="2023-12-31"
支持多种时间格式:
按作者筛选:
$ git log --author="John"
按提交信息关键词筛选:
$ git log --grep="bugfix"
组合筛选:
$ git log --author="John" --grep="bugfix"
查找特定字符串的变更历史:
$ git log -S"function_name"
$ git log -- path/to/file
查找特定条件下的提交记录:
$ git log --pretty="%h - %s" --author='Junio C Hamano' \
--since="2008-10-01" --before="2008-11-01" \
--no-merges -- t/
这个复杂查询可以:
隐藏合并提交:使用--no-merges
过滤掉合并提交,使日志更清晰
组合使用选项:大多数选项可以组合使用,如:
$ git log --oneline --graph --all
别名设置:将常用查询设置为Git别名提高效率
通过掌握这些git log
的高级用法,你可以更高效地浏览项目历史,快速定位特定变更,更好地理解项目的演进过程。这些技能对于日常开发、代码审查和问题排查都至关重要。
progit2 Pro Git 2nd Edition 项目地址: https://gitcode.com/gh_mirrors/pr/progit2