Git 基础 - 查看提交历史

本文介绍了如何使用Git命令查看提交历史和文件变更详情。通过`git log`的不同选项,如`-p`、`--stat`等,可以查看详细的提交记录及文件差异。同时,也介绍了如何对比不同分支间的提交差异。

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

git log 有许多选项可以帮助你搜寻你所要找的提交, 接下来我们介绍些最常用的。

一个常用的选项是 -p,用来显示每次提交的内容差异。 你也可以加上 -2 来仅显示最近两次提交:

$ git log -p -2  ##貌似有时候后面-2参数不用写,或者不起效果
 

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile

该选项除了显示基本信息之外,还在附带了每次 commit 的变化。 当进行代码审查,或者快速浏览某个搭档提交的 commit 所带来的变化的时候,这个参数就非常有用了。 你也可以为 git log 附带一系列的总结性选项。 比如说,如果你想看到每次提交的简略的统计信息,你可以使用 --stat 选项:

$ git log --stat

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

 Rakefile |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --graph --oneline --decorate --all
 

图形化比较显示全部不同的commit 

然后你可以根据情况查看不同的两次commit的区别

git diff 4ac0a6733 4ac0a6733 

 

git log --follow file   ##显示文件更新历史

--follow

Continue listing the history of a file beyond renames (works only for a single file).

关于git diff 输出结果分析可以参考博文:

http://www.ruanyifeng.com/blog/2012/08/how_to_read_diff.html

git log 显示各个branch 提交历史

ll changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area. This is the same set of commits that would be shown by git log <upstream>..HEAD; or by git log 'fork_point'..HEAD, if --fork-point is active (see the description on --fork-point below); or by git log HEAD, if the --root option is specified.

如上是英文说明。

其实可以通过 

git log <upstream>..HEAD 

将不同的branch联合显示。

另外想查看一个branch的提交历史可以查看

git log branch-name

参考博文:

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%AB%98%E7%BA%A7%E5%90%88%E5%B9%B6#_manual_remerge

https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2

转载于:https://my.oschina.net/u/2308739/blog/704696

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值