查看提交历史(git log)
查看提交历史(git log)
Git的提交历史记录了项目从创建到当前状态的所有变更。通过查看提交历史,可以了解代码的演变过程、定位问题来源或回溯特定版本。git log
命令是查看这些历史记录的主要工具,它提供了多种参数来定制输出格式和内容。
基本用法
最简单的git log
命令会按时间倒序列出所有提交:
git log
输出示例:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1
Author: Jane Smith <jane@example.com>
Date: Sun Oct 1 09:15:10 2023 +0800
Add user authentication middleware
常用参数
限制输出数量
使用-n
参数限制显示的提交数量:
git log -n 3
单行显示
--oneline
参数将每个提交压缩为一行:
git log --oneline
输出:
a1b2c3d Fix login page styling issues
b2c3d4e Add user authentication middleware
图形化显示分支
--graph
参数会显示ASCII图形表示分支和合并历史:
git log --graph --oneline
输出:
* a1b2c3d Fix login page styling issues
* b2c3d4e Add user authentication middleware
| * c3d4e5f Update README (feature-branch)
|/
* d4e5f6g Initial commit
筛选提交
按作者
--author
参数筛选特定作者的提交:
git log --author="John"
按时间范围
--since
和--until
参数指定时间范围:
git log --since="2023-09-01" --until="2023-09-30"
按文件变更
查看影响特定文件的提交:
git log -- path/to/file.js
查看变更详情
-p
或--patch
参数显示每次提交引入的具体变更:
git log -p
输出示例:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
diff --git a/src/components/Login.js b/src/components/Login.js
index abc1234..def5678 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -15,7 +15,7 @@ function Login() {
<form>
<div className="form-group">
- <label style={{color: 'red'}}>Username</label>
+ <label className="text-primary">Username</label>
<input type="text" />
</div>
自定义输出格式
--pretty
参数允许自定义提交信息的显示格式:
git log --pretty=format:"%h - %an, %ar : %s"
输出:
a1b2c3d - John Doe, 2 hours ago : Fix login page styling issues
b2c3d4e - Jane Smith, 3 days ago : Add user authentication middleware
常用格式占位符:
%h
:缩略提交哈希%an
:作者名字%ar
:相对时间(如"2 hours ago")%s
:提交说明%ad
:作者日期%d
:引用名称(如分支、标签)
查看文件变更统计
--stat
参数显示每次提交变更的文件和行数统计:
git log --stat
输出示例:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe <john@example.com>
Date: Mon Oct 2 14:30:22 2023 +0800
Fix login page styling issues
src/components/Login.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
高级用法
查找包含特定文本的提交
-S
参数搜索添加或删除特定文本的提交:
git log -S "useState"
查看两个提交间的差异
比较两个提交之间的历史:
git log commit1..commit2
查看分支差异
查看在feature分支但不在main分支的提交:
git log main..feature
与GUI工具结合
许多Git GUI工具提供了更直观的历史查看方式。例如在VS Code中:
- 打开源代码管理视图(Ctrl+Shift+G)
- 点击右上角的"..."菜单
- 选择"查看提交历史"
这会显示图形化的提交历史,可以点击每个提交查看详情。
实际应用示例
假设我们有一个React项目,想查看关于用户认证功能的开发历史:
git log --grep="auth" --oneline -p src/components/Auth/
这会列出所有提交说明中包含"auth"且影响Auth目录下文件的提交,并显示具体变更内容。
另一个例子,查看上周所有修改了API相关文件的提交:
git log --since="last week" --name-only -- src/api/
性能优化
对于大型仓库,git log
可能会变慢。以下优化方法:
- 限制范围:
git log -n 50
- 禁用分页:
git --no-pager log
- 使用简单格式:
git log --oneline
与其他命令结合
git log
常与其他命令结合使用。例如,找到引入bug的提交后,可以使用git show
查看详情:
git show a1b2c3d
或者用git diff
比较两个提交:
git diff b2c3d4e..a1b2c3d
在脚本中使用
git log
的输出可以用于自动化脚本。例如生成变更日志:
git log --pretty=format:"- %s (%h)" v1.0.0..HEAD > CHANGELOG.md
或者在Node.js中处理提交历史:
const { execSync } = require('child_process');
function getRecentCommits(n = 5) {
const output = execSync(`git log -n ${n} --pretty=format:"%h|%an|%s"`).toString();
return output.split('\n').map(line => {
const [hash, author, message] = line.split('|');
return { hash, author, message };
});
}
console.log(getRecentCommits());
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:移动文件(git mv)
下一篇:错误跟踪流程