如何挑选一系列提交并合并到另一个分支? [英] How to cherry pick a range of commits and merge into another branch?
问题描述
我具有以下存储库布局:
- 主分支(生产)
- 整合
- 工作
我想要实现的是从工作分支中挑选一系列提交并将其合并到集成分支中.我对git很陌生,我不知道如何准确地做到这一点(在一个操作中选择合并范围而不是合并中的提交范围)而不弄乱存储库.有任何指示或想法吗?谢谢!
当涉及到一系列提交时,挑选是 是不切实际的. >
git cherry-pick"学会了选择一系列提交
(例如"cherry-pick A..B
"和"cherry-pick --stdin
"),"git revert
"也是如此;但是,这些不支持更好的排序控件"rebase [-i]
".
在"
cherry-pick A..B
"表格中,A
应该早于B
.
如果顺序错误,该命令将自动失败.
如果您要选择范围B
至D
(包括),则该范围将为 B^..D
.
请参见"是否从以前的提交范围创建Git创建分支?".
注意:从Git 2.9.x/2.10(2016年第三季度)开始,您可以直接在孤儿分支(空头)上挑选一系列提交:请参阅"
原始答案(2010年1月) 如果您当前的分支是集成: 这将重播之间的所有内容: 到" 如果重播这些提交之一时有任何冲突: 在 选择樱桃或 如果您想使用补丁方法,则可以选择"git format-patch | git am"和"git cherry".
但是无论如何,当您需要重播"一系列提交时,重播"一词应促使您使用Git的" I have the following repository layout: What I want to achieve is to cherry pick a range of commits from the working branch and merge it into the integration branch. I pretty new to git and I can't figure out how to exactly do this (the cherry picking of commit ranges in one operation not the merging) without messing the repository up. Any pointers or thoughts on this? Thanks! When it comes to a range of commits, cherry-picking As mentioned below by Keith Kim, Git 1.7.2+ introduced the ability to cherry-pick a range of commits (but you still need to be aware of the consequence of cherry-picking for future merge) git cherry-pick" learned to pick a range of commits In the " If you want to pick the range As Jubobs mentions in the comments: This assumes that Note: as of Git 2.9.x/2.10 (Q3 2016), you can cherry-pick a range of commit directly on an orphan branch (empty head): see "How to make existing branch an orphan in git". Original answer (January 2010) A If your current branch is integration: That will replay everything between: to " If there is any conflict when one of those commits is replayed: After that With cherry-picking or A pure " If you want to use a patch approach then "git format-patch|git am" and "git cherry" are your options.
But anyway, when you need to "replay" a range of commits, the word "replay" should push you to use the " 这篇关于如何挑选一系列提交并合并到另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
rebase --onto
更好,您可以在集成分支顶部重播给定的提交范围,如 git rebase手册页,以查看git rebase --onto
)
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the integration branch to the head of the new patchset
git branch -f integration last_SHA-1_of_working_branch_range
# Rebase the patchset onto tmp, the old location of integration
git rebase --onto tmp first_SHA-1_of_working_branch_range~1 integration
first_SHA-1_of_working_branch_range
的父项之后(因此是~1
):您要重播的第一个提交integration
"(指向您要从working
分支重播的最后一个提交)tmp
"(指向integration
之前指向的位置)
git rebase --continue
". git rebase --skip
" git rebase --abort
"取消所有操作(并将integration
分支放回tmp
分支上)rebase --onto
之后,integration
将返回到集成分支的最后一次提交(即"tmp
"分支+所有重播的提交)rebase --onto
时,请不要忘记它会对后续合并产生影响,例如此处已讨论,并且涉及到以下内容:
当前,git cherry-pick
仅接受一次提交,但是如果您要选择B
至D
的范围,则在git lingo中应为B^..D
,因此
git rev-list --reverse --topo-order B^..D | while read rev
do
git cherry-pick $rev || break
done
rebase
"功能.
is was not practical.
(e.g. "cherry-pick A..B
" and "cherry-pick --stdin
"), so did "git revert
"; these do not support the nicer sequencing control "rebase [-i]
" has, though.
cherry-pick A..B
" form, A
should be older than B
.
If they're the wrong order the command will silently fail.B
through D
(inclusive) that would be B^..D
.
See "Git create branch from range of previous commits?" as an illustration.
B
is not a root commit; you'll get an "unknown revision
" error otherwise.
rebase --onto
would be better, where you replay the given range of commit on top of your integration branch, as Charles Bailey described here.
(also, look for "Here is how you would transplant a topic branch based on one branch to another" in the git rebase man page, to see a practical example of git rebase --onto
)# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the integration branch to the head of the new patchset
git branch -f integration last_SHA-1_of_working_branch_range
# Rebase the patchset onto tmp, the old location of integration
git rebase --onto tmp first_SHA-1_of_working_branch_range~1 integration
first_SHA-1_of_working_branch_range
(hence the ~1
): the first commit you want to replayintegration
" (which points to the last commit you want to replay, from the working
branch)tmp
" (which points to where integration
was pointing before)
git rebase --continue
". git rebase --skip
"git rebase --abort
" (and put back the integration
branch on the tmp
branch)rebase --onto
, integration
will be back at the last commit of the integration branch (that is "tmp
" branch + all the replayed commits)rebase --onto
, do not forget it has consequences on subsequent merges, as described here.
cherry-pick
" solution is discussed here, and would involve something like:
Currently, git cherry-pick
accepts only a single commit, but if you want to pick the range B
through D
that would be B^..D
in git lingo, so git rev-list --reverse --topo-order B^..D | while read rev
do
git cherry-pick $rev || break
done
rebase
" feature of Git.