简体   繁体   English

Git 分支差异

[英]Git branch diff

My goal is to compare two different git branches and obtain a list of the commit logs and committers responsible for every difference between them.我的目标是比较两个不同的 git 分支,并获取提交日志和负责它们之间每个差异的提交者的列表。 What is the easiest way to accomplish this?实现这一目标的最简单方法是什么?

Currently I'm doing this:目前我正在这样做:

git diff branch1..branch2

Which produces the source code that is different.这会产生不同的源代码。 I can then manually git blame the additions, but this is laborious.然后我可以手动git blame ,但这很费力。 I just want a list of all the commits responsible for differences (ideally the log and committer for each).我只想要一个负责差异的所有提交的列表(理想情况下是每个提交的日志和提交者)。

One complication, easier to explain with an example:一个复杂的问题,用一个例子更容易解释:

branch1 has commits A, B, C branch1有提交A, B, C

branch2 has commits D, E, F branch2有提交D, E, F

However, commits B and E are really the same commit (change exactly the same files--they are cherry-picks), but the log message/committer might be different.但是,提交BE实际上是相同的提交(更改完全相同的文件——它们是精选文件),但日志消息/提交者可能不同。 I don't want to include the B and E commits, I only want the changes that actually introduce differences between the branches.我不想包含BE提交,我只想要实际引入分支之间差异的更改。

if you want to compare to branches and see what are the differences in terms of commits, you are better served using git log .如果您想与分支进行比较并查看提交方面的差异,则最好使用git log为您服务。

There are many ways of using it.有很多使用它的方法。 For instance, if you want to get a list of which commits are in the origin/master and not in master , you would do:例如,如果您想获取在origin/master中而不是在master中的提交的列表,您可以这样做:

git log master..origin/master

That is called a git revision .这称为git revision There are many different ways of comparing branches, a good resource for that is this .有许多不同的方法来比较分支,是一个很好的资源。

This gives you a list of commits.这为您提供了提交列表。 You can use all the regular modifiers to see the content ( -p ), and any other information that you might want.您可以使用所有常规修饰符来查看内容 ( -p ) 以及您可能想要的任何其他信息。

You can use .. for git log as well.您也可以将..用于git log

For your case this should do对于你的情况,这应该做

git log --pretty="%h %s - %an <%ae>" branch1..branch2

Here, %h is abbreviated commit hash, %s is subject, %an author name and %ae is author email.这里, %h是 commit hash 的缩写, %s是主题, %an a 是作者姓名, %ae是作者 email。 See Pretty Formats for more options.有关更多选项,请参阅漂亮格式

For a list of commit logs:对于提交日志列表:

as others have suggested: git log a..b will give the list of commits "in b but not in a ".正如其他人所建议的那样: git log a..b将给出“在b中但不在a中”的提交列表。
You would have to look at both git log a..b and git log b..a to view the relevant commits.您必须同时查看git log a..bgit log b..a才能查看相关提交。

You can also view both in one single command: git log a...b (three dots, meaning "symmetric difference").您还可以在一个命令中查看两者: git log a...b (三个点,表示“对称差异”)。

You can add --boundary (by default, the above command does not display the commit where the branches split, adding --boundary shows it), --graph to have a better view of who is the parent of who, and --oneline if you want a compact overview of the lot:您可以添加--boundary (默认情况下,上述命令不显示分支拆分的提交,添加--boundary显示它), --graph以更好地查看谁是谁的父级,以及--oneline如果您想要对该地段的简要概述,请使用--oneline

# the --boundary and --graph are really helpful to make sense of the '...' version
git log --boundary --graph --oneline a...b

# you can use the options with any 'git log' command :
git log --graph --oneline master
git log --boundary --graph --oneline a..b

When viewing a symmetric difference, git log also has --cherry-mark and -cherry-pick options:查看对称差异时, git log也有--cherry-mark-cherry-pick选项:

# will mark with '=' commits cherry picked between the two branches :
git log --boundary --graph --cherry-mark --oneline a...b

# will hide commits cherry picked between the two branches :
git log --boundary --graph --cherry-pick --oneline a...b

Read the docs on cherry-* options for more details.阅读有关cherry-* 选项的文档以获取更多详细信息。

If you are using a graphical frontend to git, chances are you can somehow feed these options to the viewer.如果您使用 git 的图形前端,您可能会以某种方式将这些选项提供给查看器。 Most of them implicitly have the --graph option -- ie: they draw lines and dots to help you follow the history of branches and merges.它们中的大多数都隐含了--graph选项——即:它们画线和点来帮助您跟踪分支和合并的历史。

In gitk , for example, you can do this by selecting "View > New view... (Shift-F4)" from the menu, you will have a dialog, with several options available through checkboxes, and a field "Additional arguments to git log".例如,在gitk中,您可以通过从菜单中选择“查看 > 新视图...(Shift-F4)”来执行此操作,您将看到一个对话框,其中包含多个通过复选框可用的选项,以及一个字段“Additional arguments to git 日志”。


For viewing the differences:要查看差异:

If you want to view the patch introduced by each commit, you can add -p to the git log commands above:如果要查看每次提交引入的补丁,可以在上面的git log命令中添加-p

# with the following three views :
git log --graph --oneline --boundary --cherry-pick a...b
git log -p --graph --cherry b...a
git log -p --graph --cherry a...b

--cherry ( docs here ) is one of the --cherry-* variants --cherry此处的文档)是--cherry-*变体之一

You should be able to see all the info that you want (perhaps more? ;) )你应该能够看到你想要的所有信息(也许更多?;))

You might have to mix diff and blame information:您可能必须混合 diff 和 blame 信息:

https://github.com/eantoranz/difflame https://github.com/eantoranz/difflame

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

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