如何将代码从 branch1 合并到 branch2 但不包括一些提交
How to merge code from branch1 to branch2 but excluding some commits
假设我有一个repo,有两个分支,一个是master,另一个是branch2。所有提交都将进入 master,但部分提交将在 branch2 中。
例如master的提交:c1,c2,c3,e1,c4,...,e2,cn...,e3,...
如果我使用 git 合并,那么所有提交都将在 branch2 中。但我想排除 e1、e2、e3 等
有没有有效的方法?谢谢。
您可以通过两种方式完成此操作。第一种方式是在交互模式下使用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
可能是最佳选择。挑选许多提交很容易出错,因为您必须以正确的顺序手动指定所有这些,并且您必须记住要排除哪些。
假设我有一个repo,有两个分支,一个是master,另一个是branch2。所有提交都将进入 master,但部分提交将在 branch2 中。
例如master的提交:c1,c2,c3,e1,c4,...,e2,cn...,e3,... 如果我使用 git 合并,那么所有提交都将在 branch2 中。但我想排除 e1、e2、e3 等
有没有有效的方法?谢谢。
您可以通过两种方式完成此操作。第一种方式是在交互模式下使用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
可能是最佳选择。挑选许多提交很容易出错,因为您必须以正确的顺序手动指定所有这些,并且您必须记住要排除哪些。