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

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

问题描述

我在一个新项目中使用 Git,该项目有两个并行的——但目前处于实验阶段——开发分支:

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

  • master:导入现有的代码库加上一些我通常确定的修改
  • exp1:实验分支#1
  • exp2:实验分支#2
  • master: import of existing codebase plus a few modifications 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.

手动将公共文件复制到临时目录,然后 git checkout 移动到另一个分支,然后从临时目录手动复制到工作树中.

Manual copying of common files into a temporary directory followed by git checkout to move to the other branch and then more manual copying out of the temporary 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.

在 Red Hat Magazine 中还有另一种不错的方法,他们使用 git add --patchgit add --interactive> 如果您想将不同的更改拆分到单个文件(在该页面中搜索拆分"),它允许您仅添加大块的一部分.

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天全站免登陆