如何将branch1中的代码合并到branch2,但不包括一些提交 [英] How to merge code from branch1 to branch2 but excluding some commits

查看:276
本文介绍了如何将branch1中的代码合并到branch2,但不包括一些提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个回购协议,并且有两个分支,一个是master,另一个是branch2。所有的提交都将成为master,但部分提交将在branch2中。例如,master的commit:c1,c2,c3,e1,c4,...,e2,cn ...,e3,....
如果我使用git merge,那么所有提交将在branch2中。但我想排除e1,e2,e3等。



有没有可能有效的方法?感谢。

解决方案

您可以通过两种方式来完成此操作。第一种方法是在交互模式下使用 git-rebase

  git checkout branch2 
git rebase -i master

你会看到一些类似的东西这:

  pick a8xl3do提交c1 
的评论pick 49Un2Sa提交c2的评论
pick d0a9iUl评论提交c3
选择G38c8oA评论提交e1

Git会给你一个选项列表,包括以下评论:

 #如果你在这里删除一行,那么COMMIT将会丢失。 

您可以删除与您不想要的提交相对应的行(e1,e2,e3,等等),然后完成rebase。



您的情况的另一种选择是使用 git cherry-pick 。使用> cherry-pick 你可以基本上从你想要的 master 中提交提交。继续我们上面的例子:

  git checkout branch2 
git cherry-pick a8xl3do
git cherry-选择49Un2Sa
git cherry-pick d0a9iUl
git cherry-pick G38c8oA
#但是不要选择提交e1,e2,e3等的SHA1哈希

如果您正在处理大量提交,那么 rebase 是可能是要走的路。樱桃挑选许多提交很容易出错,因为你必须按照正确的顺序手动指定所有提交,并且必须记住要排除哪些提交。


Suppose I have a repo, and there are two branches, one is master, the other one is branch2. All the commits will be into master, but part of the commits will be in branch2.

For example, master's commit: c1, c2, c3, e1, c4, ..., e2, cn..., e3, .... If I use git merge, then all the commit will be in branch2. But I want to excludes e1, e2, e3, etc.

Is it possible to have an effective way? Thanks.

解决方案

There are two ways that you can go about doing this. The first way is to use git-rebase in interactive mode.

git checkout branch2
git rebase -i master

You will be prompted with a something looking like this:

pick a8xl3do Comment for commit c1
pick 49Un2Sa Comment for commit c2
pick d0a9iUl Comment for commit c3
pick G38c8oA Comment for commit e1

Git will give you a list of options, including the following comment:

# If you remove a line here THAT COMMIT WILL BE LOST.

You can delete the lines corresponding to the commits you don't want (e1, e2, e3, etc.) and then finish the rebase.

The other option for your situation is to use git cherry-pick. Using cherry-pick you would basically hand-pick the commits from master which you want. Continuing with our example from above:

git checkout branch2
git cherry-pick a8xl3do
git cherry-pick 49Un2Sa
git cherry-pick d0a9iUl
git cherry-pick G38c8oA
# But don't cherry-pick the SHA1 hashes for commits e1, e2, e3, etc.

If you are dealing with a lot of commits, then rebase is probably the way to go. Cherry-picking many commits is error prone, because you have to manually specify all of them, in the right order, and you have to remember which ones to exclude.

这篇关于如何将branch1中的代码合并到branch2,但不包括一些提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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