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

发布于 2024-09-07 01:21:10 字数 454 浏览 4 评论 0原文

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

让我在 master 分支中有 payment.php (以及许多其他文件)。我有一个 paypal 分支,它只有一个文件,即 payment.php。现在我修复了 master 的 payment.php 中的一个错误,并希望将此修复合并到 paypal 分支中。但是,当我运行合并时,绝对所有文件都会添加到该分支中。所以最后 paypal 分支拥有 master 分支的所有文件。您知道如何解决这个问题吗?我希望 GIT 仅合并此分支中存在的文件,因此在上面的示例中,paypal 分支应该仍然只有一个文件 ( payment.php ),并合并了错误修复。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最终幸福 2024-09-14 01:21:10

这就是为什么管理好分支很重要,特别是使用主题分支和向上合并。

您应该在主题分支上进行修复,该主题分支是从需要修复的所有分支的共同祖先分叉出来的,然后将其合并到 master 和 paypal 中:

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

如果您已经进行了错误修复,并且您错误地在 master 上进行了修复来自适当的合并基础,并且 master 上的历史记录尚未发布,您应该将其挑选或重新设置到正确的位置:

# 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

如果历史记录已经已发布,您所能做的就是将错误修复挑选到 paypal 分支上,并记住下次正确执行:

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

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>
酒中人 2024-09-14 01:21:10

为什么您的其他分支不包含属于 master 分支的所有文件?通过删除所有其他文件的麻烦,您只会让事情变得更加困难。特别是如果您后来最终需要更改已删除的其他文件。

Why don't your other branches contain all the files that are part of the master branch? 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.

尸血腥色 2024-09-14 01:21:10

你做错了。您应该拥有分支中的所有文件。

跟踪分支之间哪些文件不同是 Git 的工作,而不是你的工作。
这就是使用 VCS 的要点之一。

如果您只想分发分支之间不同的文件,则可以通过脚本轻松提取这些文件。

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.
That's sort of one of the points of using a VCS.

If you want to distribute just the files that differ between branches, those can easily be extracted by a script.

柏拉图鍀咏恒 2024-09-14 01:21:10

您可以执行类似的操作: http://nvie.com/git-model

(我希望这有效)

master 将继续作为您的主分支。您创建了第二个名为 bugfix 的 master 分支。您创建了名为 plugin-foo 的 bug 修复的第三个分支。

在plugin-foo 中,您删除所有不需要的文件。现在,每当您对不在插件分支中的文件进行更改时,您都会在主分支上进行这些更改。所有错误修复都会进入错误修复分支。您定期将错误修复分支合并到主分支和插件分支中。这会导致这两个分支都出现错误修复。

You can do something similar to this: http://nvie.com/git-model

(I hope this works)

master will continue to be your main branch. You create a second branch off master called bugfix. You create a third branch off bugfix called plugin-foo.

In plugin-foo you delete all the files that are not needed. Now, whenever you make a change to the files that are not in the plugin branch, you do those on the master branch. All bugfixes go into the bugfix branch. You periodically merge the bugfix branch into both master and the plugin branches. Which leads to bugfixes going into both of those branches.

定格我的天空 2024-09-14 01:21:10

将插件隔离在自己的 git 存储库中允许您在父项目上独立地对它们进行改进。

但是,如果您需要将它们直接包含在项目中,则最近的 Git 版本(git1.7.11,2012 年 6 月)包含 git 子树脚本(之前开发于GitHub by apenwarr,现已合并到主线 git 中)

这样,您可以合并一个存储库(及其历史)在另一个中,保留稍后提取其历史记录的选项(而不是子树合并)。
这可以被视为 git 子模块的替代方案。

另一种选择是 git Slave,以保持父存储库和子模块紧密同步。

Isolating your plugins in their own git repo allows you to make evolutions on them independently on a parent project.

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)

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.

Another alternative is git slave, to keep parent repo and submodules tightly in sync.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文