如何在git中看到分支之间的提交差异?

本文详细介绍如何使用Git命令轻松查看两个分支间的提交差异,包括使用git log、git diff、gitk等工具,以及如何配置gitlog以获得更好的输出效果。

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

本文翻译自:How do I see the commit differences between branches in git?

I'm on branch-X and have added a couple more commits on top of it. 我在分支-X上,并在其上添加了几个提交。 I want to see all the differences between MASTER and the branch that I am on in terms of commits. 我想看看MASTER和我提交的分支之间的所有区别。 I could just do a 我可以做一个

git checkout master
git log

and then a 然后一个

git checkout branch-X
git log

and visually diff these, but I'm hoping for an easier, less error-prone method. 并且在视觉上区分这些,但我希望有一种更简单,更不容易出错的方法。


#1楼

参考:https://stackoom.com/question/WB2f/如何在git中看到分支之间的提交差异


#2楼

You can easily do that with 你可以轻松地做到这一点

git log master..branch-X

That will show you commits that branch-X has but master doesn't. 这将显示你提交的分支-X但主却没有。


#3楼

你可以得到一个非常好的视觉输出,你的分支与此有何不同

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative master..branch-X

#4楼

If you are on Linux, gitg is way to go to do it very quickly and graphically. 如果您使用的是Linux, gitg可以非常快速地以图形方式完成。

If you insist on command line you can use: 如果你坚持使用命令行,你可以使用:

git log --oneline --decorate

To make git log nicer by default, I typically set these global preferences: 为了使git log默认更好,我通常设置这些全局首选项:

git config --global log.decorate true
git config --global log.abbrevCommit true

#5楼

if you want to use gitk: 如果你想使用gitk:

gitk master..branch-X

it has a nice old school GUi 它有一个很好的老派GUi


#6楼

I think it is matter of choice and context.I prefer to use 我认为这是选择和背景的问题。我更喜欢使用

git log origin/master..origin/develop --oneline --no-merges

It will display commits in develop which are not in master branch. 它将显示不在master分支中的develop中的提交。

If you want to see which files are actually modified use 如果要查看实际修改了哪些文件,请使用

git diff --stat origin/master..origin/develop --no-merges

If you don't specify arguments it will display the full diff. 如果不指定参数,它将显示完整的差异。 If you want to see visual diff, install meld on linux, or WinMerge on windows. 如果你想看到visual diff,请在linux上安装meld ,或在windows上安装WinMerge Make sure they are the default difftools .Then use something like 确保它们是默认的difftools。然后使用类似的东西

git difftool -y origin/master..origin/develop --no-merges

In case you want to compare it with current branch. 如果您想将其与当前分支进行比较。 It is more convenient to use HEAD instead of branch name like use: 使用HEAD代替分支名称比使用更方便:

git fetch
git log origin/master..HEAD --oneline --no-merges

It will show you all the commits, about to be merged 它将向您显示即将合并的所有提交

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值