git 获取Git中两个分支的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16675766/
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 16:11:41  来源:igfitidea点击:

Get the difference between two branches in Git

git

提问by Victor Ronin

I did following (I simplified this comparing to a reality):

我做了以下(与现实相比,我简化了这一点):

  • created a branch Branch1, switched to it
  • added file File1and modified existing file File2and commited this
  • figured out that I don't need File1, removed it and commited this
  • 创建了一个分支Branch1,切换到它
  • 添加文件File1并修改现有文件File2并提交此
  • 发现我不需要File1,将其删除并提交

So, the actual difference between original branchand Branch1is only modification of File2.

因此,原始分支Branch1之间的实际区别只是File2 的修改。

I want to get this actual difference between branches and put in Branch2. Generally speaking, I want to get rid of not necessary history of adding/removing File1.

我想获得分支之间的实际差异并放入Branch2。一般来说,我想摆脱添加/删除File1 的不必要历史记录。

回答by Klas Mellbourn

Let's assume that you started on the branch master. Then you can do:

假设您从分支开始master。然后你可以这样做:

git diff master Branch1 > ../patchfile
git checkout Branch2    
git apply ../patchfile

Alternatively, if your goal is to rewrite history, then you could use an interactive rebaseto squash commits.

或者,如果您的目标是重写历史记录,那么您可以使用交互式 rebase来压缩提交。

回答by MaxK

This is a simple git diff

这是一个简单的 git diff

git diff --name-only SHA1 SHA2

Where SHA1/2 are the hashes of the 2 commits at the top of each branch.

其中 SHA1/2 是每个分支顶部的 2 个提交的哈希值。

Or you can do

或者你可以做

git diff --name-only develop...

To compare your branch against the developbranch

将您的分支与develop分支进行比较

回答by sebastian

I would do an interactive rebase on HEAD~2and squash the last two commits together. Assuming that you want to keep the history as is in Branch1and simplify it in Branch2, do the following (current branch Branch1):

我会做一个交互式 rebaseHEAD~2并将最后两个提交压缩在一起。假设您想保留历史记录并在 中Branch1简化它Branch2,请执行以下操作(当前分支Branch1):

git checkout -b Branch2
git rebase -i 'HEAD~2'

An editor will open up, showing something like

一个编辑器将打开,显示类似

pick 1b58da0 Added File1, changed File2
pick d3f4f51 Delete File1

and many explanatory comments how rebasing works. Change the last commit to a squashand close the editor.

以及许多解释性评论 rebase 是如何工作的。将最后一次提交更改为 asquash并关闭编辑器。

pick 1b58da0 Added File1, changed File2
squash d3f4f51 Delete File1

A new editor will open up where you can specify the new commit message. It would probably now just read

将打开一个新编辑器,您可以在其中指定新的提交消息。它现在可能只是阅读

Changed File2

更改文件 2

Close it and you're done, both commits are squashed together on Branch2and Branch1retains your original history. Note that if you don't need to retain the original history, you can just skip checking out Branch2and work directly on Branch1. Only do that if you haven't published your last two commits on Branch1already.

关闭它,你就完成了,两个提交都被压缩在一起Branch2Branch1保留你的原始历史。请注意,如果您不需要保留原始历史记录,则可以跳过签出Branch2并直接在Branch1. 仅当您尚未发布最后两次提交时才这样做Branch1