Git:如何合并修改过的文件 [英] Git: how to merge modified files only

查看:207
本文介绍了Git:如何合并修改过的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:
我们有一个在master分支中的大产品。此外,我们还有其他只有少数文件的分支,这些分支仅限于此分支。每个分支都代表主要产品的插件。因此,例如,当您获得主要产品时,您会收到大量文件,安装它们等,稍后当您决定获得插件时,您将收到一个仅包含多个文件的包,并通过上传这些文件(并替换原始文件)你会得到安装的插件。

让我们在master分支(以及许多其他文件)中使用payment.php。我有paypal分支,其中只有一个文件是payment.php。现在我修复了master的payment.php中的一个错误,并且想要将此修复合并到paypal分支中。但是,当我运行合并时,绝对所有文件都被添加到该分支。所以最后,贝宝分支有来自主分支的所有文件。你偶然知道如何解决这个问题吗?我希望GIT合并仅存在于此分支中的文件,因此在上面的示例中,贝宝​​分支应该仍然只有一个文件(payment.php),并将错误修复合并到其中。



p>您应该修复一个主题分支,从所有需要该修补程序的分支的共同祖先中分出,然后将其合并到master和paypal中:

  x  -  x  -  x  -  x ------------- X(master)
| \ |
| x - x - x ---- X(paypal)|
\\ / /
x(bugfix)---------------

如果您已经完成了错误修正,并且您错误地使用了它而不是从合适的合并基础开始,并且关于master的历史记录尚未发布,那么您应该挑选或重新绑定它到了正确的位置:

 #如果bugfix提交不在master的顶端,那么可以重新绑定以获取它:
git rebase -i<在错误修复之前提交> master
#重新排列提交列表,将bug修复放在提示处,保存并退出

#现在,选择樱桃或将其提交到正确的位置
#(如果bugfix实际上是多次提交,rebase就更容易了)

#Cherry-pick
#做一个分支和樱桃挑选
git checkout -b bugfix<合并基础>>的SHA1 ;
git cherry-pick< bug修正的SHA1>
#从主服务器中删除提交,假设它仍然在提示中
git checkout master
git reset --hard master ^

#或rebase
#make the bugfix branch(假设它仍然在提示中)
git branch bugfix master
#并从master中移除提交(假设它仍然在提示中)
git checkout master
git reset --hard master ^#或者如果错误修复由n个提交组成,那么master〜n
#将错误修复分支重新绑定到正确的位置
git rebase --onto< SHA1 of merge base> ; master bugfix

如果历史记录已发布,您所能做的就是樱桃选择错误修正到贝宝分支,并记得在下次做正确的:

  git checkout paypal 
git cherry-pick< bug修正的SHA1>


I have the following problem: We have a large product which is in master branch. Also we have other branches that have only few files, the files that are specific to this branch only. Each of those branches represent a plugin to the main product. So for example when you get the main product, you receive lots of files, install them, etc. and later when you decide to get a plugin, you receive a package containing several files only and by uploading these file (and replacing the original ones) you get the plugin installed.

Let's I have payment.php in master branch (as well as many other files). And I have paypal branch which has one file only which is payment.php. Now I fix a bug in master's payment.php and want to merge this fix in paypal branch. However when I run merge, absolutely all files get added to that branch. So in the end paypal branch has all the files from master branch. Do you by chance know how this can be fixed? I want GIT to merge the files that exist in this branch only, so in the example above the paypal branch should still have one file only (payment.php) with the bug fix merged in.

解决方案

This is why it's important to manage your branches well, in particular to use topic branches and to merge upwards.

You should make that fix on a topic branch, forked from a common ancestor of all branches which will need the fix, and then merge it into both master and paypal:

x - x - x - x ------------- X (master)
|\                          |
| x - x - x ---- X (paypal) |
 \              /           /
  x (bugfix) ---------------

If you have already made your bugfix, and you mistakenly made it on master instead of from the appropriate merge base, and the history on master hasn't been published, you should cherry-pick or rebase it to the right place:

# If the bugfix commit is not at the tip of master, you can rebase to get it there:
git rebase -i <commit before the bugfix> master
# rearrange the list of commits to put the bugfix at the tip, save and quit

# Now either cherry-pick or rebase the commit to the right place
# (rebase is easier if the bugfix is actually several commits)

# Cherry-pick
# make a branch and cherry-pick
git checkout -b bugfix <SHA1 of merge base>
git cherry-pick <SHA1 of bugfix>
# remove the commit from master, assuming it's still on the tip
git checkout master
git reset --hard master^

# or rebase
# make the bugfix branch (assuming it's still on the tip)
git branch bugfix master
# and remove the commit from master (assuming it's still on the tip)
git checkout master
git reset --hard master^    # or if the bugfix is composed of n commits, master~n
# rebase the bugfix branch to the right place
git rebase --onto <SHA1 of merge base> master bugfix

If the history has been published, all you can do is cherry-pick the bugfix onto the paypal branch, and remember to do it right the next time:

git checkout paypal
git cherry-pick <SHA1 of bugfix>

这篇关于Git:如何合并修改过的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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