Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

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

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

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

  • master : import of existing codebase plus a few mods that I'm generally sure of

    (master :导入现有的代码库以及一些我通常确定的mod)

  • exp1 : experimental branch #1

    (exp1 :实验分支1)

  • exp2 : experimental branch #2

    (exp2 :实验分支2)

exp1 and exp2 represent two very different architectural approaches.

(exp1exp2代表两种非常不同的体系结构方法。)

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.

(当我在一个分支中取得进展时,有时我会进行一些编辑,而这些编辑在另一分支中将非常有用,并且希望将这些合并。)

What is the best way to merge selective changes from one development branch to another while leaving behind everything else?

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

Approaches I've considered:

(我考虑过的方法:)

  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 merge --no-commit然后手动取消暂存我不想在分支之间共享的大量编辑。)

  2. 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.

    (手动将常用文件复制到temp目录中,然后git checkout移至另一个分支,然后从temp目录中进行更多手动复制到工作树中。)

  3. A variation on the above.

    (上面的变化。)

    Abandon the exp branches for now and use two additional local repositories for experimentation.

    (现在放弃exp分支,并使用另外两个本地存储库进行实验。)

    This makes the manual copying of files much more straightforward.

    (这使得手动复制文件更加简单。)

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.

(类似于filter path参数,可以使git-merge更具选择性。)

  ask by David Joyner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the exact same problem as mentioned by you above.

(我遇到了与您上面提到的完全相同的问题。)

But I found this clearer in explaining the answer.

(但是我在解释答案时发现了这一点 。)

Summary:

(摘要:)

  • Checkout the path(s) from the branch you want to merge,

    (从您要合并的分支中签出路径,)

     $ git checkout source_branch -- <paths>... 

    Hint: It also works without -- like seen in the linked post.

    (提示:也可以在没有链接的情况下使用--就像在链接文章中看到的那样。)

  • or to selectively merge hunks

    (或有选择地合并大块)

     $ git checkout -p source_branch -- <paths>... 

    Alternatively, use reset and then add with the option -p ,

    (或者,使用reset,然后添加选项-p ,)

     $ git reset <paths>... $ git add -p <paths>... 
  • Finally commit

    (最后提交)

     $ git commit -m "'Merge' these changes" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...