简体   繁体   English

Git:如何仅合并修改的文件

[英]Git: how to merge modified files only

I have the following problem: We have a large product which is in master branch. 我有以下问题:我们在master分支中有一个大型产品。 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). 让我们在master分支中有payment.php(以及许多其他文件)。 And I have paypal branch which has one file only which is payment.php. 我有贝宝(Paypal)分支,该分支只有一个文件是payment.php。 Now I fix a bug in master's payment.php and want to merge this fix in paypal branch. 现在,我修复了master的payment.php中的一个错误,并希望将此合并到paypal分支中。 However when I run merge, absolutely all files get added to that branch. 但是,当我运行merge时,绝对所有文件都会添加到该分支中。 So in the end paypal branch has all the files from master branch. 因此,最后,贝宝分支具有master分支的所有文件。 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. 我希望GIT仅合并存在于此分支中的文件,因此在上面的示例中,贝宝​​分支仍应仅包含一个文件(payment.php),并合并了错误修复程序。

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: 您应该在需要该修复程序的所有分支的共同祖先分支的主题分支上进行此修复,然后将其合并到master和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: 如果历史上已经公布,所有你能做的就是樱桃挑bug修正到贝宝分公司,并记住下一次做正确的事:

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

Why don't your other branches contain all the files that are part of the master branch? 为什么您的其他分支不包含master分支中的所有文件? You're just making things harder on yourself by going through the trouble of removing all the other files. 通过消除所有其他文件的麻烦,您使事情变得更加艰难。 Especially if you later end up needing to change some other file that you already removed. 尤其是如果您以后需要更改已经删除的其他文件时,尤其如此。

You're doing it wrong. 你这样做是错的。 You're supposed to have all the files in the branch. 您应该在分支中拥有所有文件。

It's Git's job to keep track of which files differ between the branches, not yours. 跟踪分支之间(而不是分支之间)哪些文件不同是Git的工作。 That's sort of one of the points of using a VCS. 这就是使用VCS的要点之一。

If you want to distribute just the files that differ between branches, those can easily be extracted by a script. 如果只想分发分支之间不同的文件,则可以通过脚本轻松提取这些文件。

Isolating your plugins in their own git repo allows you to make evolutions on them independently on a parent project. 将插件隔离在自己的git repo中,可以让您在父项目上独立地对其进行开发。

If, however, you need them directly included in your project, recent Git releases (git1.7.11 ,June 2012), includes the git subtree script (previously developed on GitHub by apenwarr , now merged into mainline git) 但是,如果您需要将它们直接包含在项目中,则最新的Git版本(git1.7.11,2012年6月)包括git子树脚本 (先前由apenwarr 在GitHub开发 ,现已合并到主线git中)

That way, you can merge one repo (and its history) in another, keeping the option to extract its history later on (as opposed to the subtree merge). 这样,您可以将一个存储库(及其历史记录)合并到另一个存储库中,并保留以后提取其历史记录的选项(与子树合并相反)。
That could be seen as an alternative to git submodules. 可以将其视为git子模块的替代方案。

Another alternative is git slave , to keep parent repo and submodules tightly in sync. 另一个选择是git slave ,以保持父仓库和子模块紧密同步。

You can do something similar to this: http://nvie.com/git-model 您可以执行以下操作: http : //nvie.com/git-model

(I hope this works) (我希望这可行)

master will continue to be your main branch. master将继续是您的主要分支。 You create a second branch off master called bugfix . 您从master创建了第二个分支,称为bugfix You create a third branch off bugfix called plugin-foo . 您从Bugfix创建了第三个分支,称为plugin-foo

In plugin-foo you delete all the files that are not needed. 在plugin-foo中,删除所有不需要的文件。 Now, whenever you make a change to the files that are not in the plugin branch, you do those on the master branch. 现在,每当对plugin分支中没有的文件进行更改时,都可以在master分支中进行。 All bugfixes go into the bugfix branch. 所有错误修正均进入错误修正分支。 You periodically merge the bugfix branch into both master and the plugin branches. 您会定期将bugfix分支合并到master和plugin分支中。 Which leads to bugfixes going into both of those branches. 这导致错误修正进入这两个分支。

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

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