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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13965391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:07:41  来源:igfitidea点击:

How do I see the commit differences between branches in git?

gitloggingbranchgit-log

提问by Avery

I'm on branch-X and have added a couple more commits on top of it. I want to see all the differences between MASTER and the branch that I am on in terms of commits. I could just do a

我在分支 X 上,并在它之上添加了几个提交。我想查看 MASTER 和我所在的分支在提交方面的所有差异。我可以做一个

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.

并在视觉上区分这些,但我希望有一种更简单、更不容易出错的方法。

采纳答案by tom

You can get a really nice, visual output of how your branches differ with this

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

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

回答by Pablo Fernandez heelhook

You can easily do that with

你可以很容易地做到这一点

git log master..branch-X

That will show you commits that branch-X has but master doesn't.

这将显示 branch-X 有但 master 没有的提交。

回答by zainengineer

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 meldon linux, or WinMergeon windows. Make sure they are the default difftools .Then use something like

如果您不指定参数,它将显示完整的差异。如果您想查看视觉差异,请meld在 linux 或WinMergewindows 上安装。确保它们是默认的 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

它将显示所有即将合并的提交

回答by mvp

If you are on Linux, gitgis 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 lognicer by default, I typically set these global preferences:

为了在git log默认情况下更好,我通常设置这些全局首选项:

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

回答by mmaruska

I'd suggest the following to see the difference "in commits". For symmetric difference, repeat the command with inverted args:

我建议以下内容来查看“提交中”的差异。对于对称差异,请使用反向参数重复该命令:

git cherry -v master [your branch, or HEAD as default]

回答by snowcamel

if you want to use gitk:

如果你想使用 gitk:

gitk master..branch-X

it has a nice old school GUi

它有一个漂亮的老式 GUI

回答by AIon

Not the perfect answer but works better for people using Github:

不是完美的答案,但更适合使用 Github 的人:

enter image description here

enter image description here

Go to your repo: Insights -> Network

转到您的回购: Insights -> Network

回答by Maroun

If you want to compare based on the commit messages, you can do the following:

如果要根据提交消息进行比较,可以执行以下操作:

git fetch
git log --oneline origin/master | cut -d' ' -f2- > master_log
git log --oneline origin/branch-X | cut -d' ' -f2- > branchx_log
diff <(sort master_log) <(sort branchx_log)

回答by claudiu.f.marginean

I used some of the answers and found one that fit my case ( make sure all tasks are in the release branch).

我使用了一些答案并找到了一个适合我的情况(确保所有任务都在发布分支中)。

Other methods works as well but I found that they might add lines that I do not need, like merge commits that add no value.

其他方法也有效,但我发现它们可能会添加我不需要的行,例如不添加任何值的合并提交。

git fetch
git log origin/master..origin/release-1.1 --oneline --no-merges

or you can compare your current with master

或者您可以将您的当前与主人进行比较

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

git fetchis there to make sure you are using updated info.

git fetch是否可以确保您使用的是更新的信息。

In this way each commit will be on a line and you can copy/paste that into an text editor and start comparing the tasks with the commits that will be merged.

通过这种方式,每个提交都在一行上,您可以将其复制/粘贴到文本编辑器中,并开始将任务与将合并的提交进行比较。

回答by Kim Briggs

#! /bin/bash
if ((2==$#)); then
  a=
  b=
  alog=$(echo $a | tr '/' '-').log
  blog=$(echo $b | tr '/' '-').log
  git log --oneline $a > $alog
  git log --oneline $b > $blog
  diff $alog $blog
fi

Contributing this because it allows a and b logs to be diff'ed visually, side by side, if you have a visual diff tool. Replace diff command at end with command to start visual diff tool.

贡献这个是因为它允许 a 和 b 日志在视觉上并排比较,如果你有一个视觉差异工具。将末尾的 diff 命令替换为启动可视化差异工具的命令。