获取Git中两个分支之间的差异 [英] Get the difference between two branches in Git

查看:795
本文介绍了获取Git中两个分支之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下(我简化了这个与现实的比较):


  • 创建了一个分支 Branch1 ,切换到它

  • 添加了文件 File1 并修改了现有文件 File2 并提交了此文件

  • 发现我并不需要 File1 ,删除它并提交了这个



所以,实际差异原始分支 Branch1 之间仅为 File2 的修改。 获得分支机构之间的实际差异并将其放入 Branch2 中。一般来说,我想摆脱没有必要添加/删除 File1 的历史记录。 解决方案

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

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

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


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

  • created a branch Branch1, switched to it
  • added file File1 and modified existing file File2 and commited this
  • figured out that I don't need File1, removed it and commited this

So, the actual difference between original branch and Branch1 is only modification of 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.

解决方案

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

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 rebase to squash commits.

这篇关于获取Git中两个分支之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆