假设我有一个repo,并且有两个分支,一个是master,另一个是branch2。所有提交都将成为master,但部分提交将在branch2中。
例如,master的提交:c1,c2,c3,e1,c4,...,e2,cn ......,e3,.... 如果我使用git merge,那么所有提交都将在branch2中。但我想排除e1,e2,e3等。
有可能有效吗?感谢。
答案 0 :(得分:3)
有两种方法可以做到这一点。第一种方法是在交互模式下使用git-rebase
。
git checkout branch2
git rebase -i master
系统会提示您显示如下内容:
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会给你一个选项列表,包括以下评论:
# If you remove a line here THAT COMMIT WILL BE LOST.
您可以删除与您不想要的提交相对应的行(e1,e2,e3等),然后完成变基。
您的情况的另一个选择是使用git cherry-pick
。使用cherry-pick
,您基本上可以从master
手动选择您想要的提交。继续我们上面的例子:
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.
如果你正在处理很多提交,那么rebase
可能是要走的路。挑选多个提交很容易出错,因为你必须按正确的顺序手动指定所有提交,并且你必须记住要排除哪些提交。