如何有选择地合并或选择Git中另一个分支的更改? [英] How to selectively merge or pick changes from another branch in Git?

查看:254
本文介绍了如何有选择地合并或选择Git中另一个分支的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个新项目上使用git,该项目具有两个并行的但目前仍处于试验阶段的开发分支:

I'm using git on a new project that has two parallel -- but currently experimental -- development branches:

  • master:导入现有代码库以及我通常确定的一些mods
  • exp1:实验分支#1
  • exp2:实验分支#2
  • master: import of existing codebase plus a few mods that I'm generally sure of
  • exp1: experimental branch #1
  • exp2: experimental branch #2

exp1exp2代表两种非常不同的体系结构方法.直到我进一步了解为止,我还无法知道哪一个(如果有的话)会起作用.当我在一个分支中取得进展时,有时我会进行一些编辑,而这些编辑在另一分支中将非常有用,并且希望将这些合并.

exp1 and exp2 represent two very different architectural approaches. Until I get further along I have no way of knowing which one (if either) will work. As I make progress in one branch I sometimes have edits that would be useful in the other branch and would like to merge just those.

将选择的更改从一个开发分支合并到另一个开发分支,同时保留其他所有内容的最佳方法是什么?

我考虑过的方法:

  1. git merge --no-commit,然后手动取消暂存我不想在分支之间共享的大量编辑.

  1. git merge --no-commit followed by manual unstaging of a large number of edits that I don't want to make common between the branches.

将公用文件手动复制到temp目录,然后按git checkout移到另一个分支,然后再从temp目录手动复制到工作树.

Manual copying of common files into a temp directory followed by git checkout to move to the other branch and then more manual copying out of the temp directory into the working tree.

上述内容的一种变化.现在放弃exp分支,并使用另外两个本地存储库进行实验.这使得手动复制文件更加简单.

A variation on the above. Abandon the exp branches for now and use two additional local repositories for experimentation. This makes the manual copying of files much more straightforward.

这三种方法似乎都很乏味且容易出错.我希望有更好的方法;类似于过滤器路径参数,可以使git-merge更具选择性.

All three of these approaches seem tedious and error-prone. I'm hoping there is a better approach; something akin to a filter path parameter that would make git-merge more selective.

推荐答案

您使用 cherry-pick 命令从一个分支获取单个提交.

You use the cherry-pick command to get individual commits from one branch.

如果所需的更改不在单独的提交中,请使用此处显示的方法提交到单个提交中.粗略地说,您使用git rebase -i进行原始提交进行编辑,然后使用git reset HEAD^有选择地还原更改,然后使用git commit将该位作为历史记录中的新提交进行提交.

If the change(s) you want are not in individual commits, then use the method shown here to split the commit into individual commits. Roughly speaking, you use git rebase -i to get the original commit to edit, then git reset HEAD^ to selectively revert changes, then git commit to commit that bit as a new commit in the history.

There is another nice method here in Red Hat Magazine, where they use git add --patch or possibly git add --interactive which allows you to add just parts of a hunk, if you want to split different changes to an individual file (search in that page for "split").

对更改进行拆分后,您现在就可以选择所需的内容了.

Having split the changes, you can now cherry-pick just the ones you want.

这篇关于如何有选择地合并或选择Git中另一个分支的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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