简体   繁体   English

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

[英]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. 并且在视觉上区分这些,但我希望有一种更简单,更不容易出错的方法。

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但主却没有。

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

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

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 它将向您显示即将合并的所有提交

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

I'd suggest the following to see the difference "in commits". 我建议以下内容以查看“提交中”的区别。 For symmetric difference, repeat the command with inverted args: 对于对称差异,请使用反向args重复命令:

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

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

gitk master..branch-X

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

Not the perfect answer but works better for people using Github: 不是完美的答案,但对于使用Github的人来说效果更好

在此输入图像描述

Go to your repo: Insights -> Network 转到您的仓库: Insights -> Network

#! /bin/bash
if ((2==$#)); then
  a=$1
  b=$2
  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. 如果您有视觉差异工具,它可以使a和b日志在视觉上并排显示,从而有助于实现这一点。 Replace diff command at end with command to start visual diff tool. 使用命令在结尾处替换diff命令以启动可视化差异工具。

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 fetch is 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. 通过这种方式,每个提交都将在一行上,您可以将其复制/粘贴到文本编辑器中,并开始将任务与将要合并的提交进行比较。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM