git Git从以前的提交范围创建分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9853681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git create branch from range of previous commits?
提问by Steven
I have a bunch of commits for an OS project and I want to pull out just the last say 20 commits into another branch so I can pull request.
我有一堆针对 OS 项目的提交,我想将最后说的 20 个提交提取到另一个分支中,以便我可以拉取请求。
How could I do this? The reason being is I have maybe 150 commits, but most of those are for a much larger contribute, which isn't ready yet. But the next version is being released soon.
我怎么能这样做?原因是我可能有 150 次提交,但其中大部分是为了更大的贡献,还没有准备好。但是下一个版本很快就会发布。
Thanks!
谢谢!
回答by Amber
You can do this with cherry-pick
. If your history of long_branch
looks like this:
您可以使用cherry-pick
. 如果你的历史long_branch
看起来像这样:
A-B <-- master
\
C-D-E-F-G-H <-- long_branch
and you want to move the contents of, say, F through H to a different branch, say, short_branch
, which is based off of master
:
并且您想将 F 到 H 的内容移动到不同的分支,例如,short_branch
,它基于以下内容master
:
git checkout master -b short_branch
which gives
这使
A-B <-- master, short_branch
\
C-D-E-F-G-H <-- long_branch
then... (note that the commit range is E..H
; the left side is non-inclusive for the range)
然后...(请注意,提交范围是E..H
;左侧不包含该范围)
git cherry-pick E..H
which gives:
这使:
F'-G'-H' <-- short_branch
/
A-B <-- master
\
C-D-E-F-G-H <-- long_branch
Note that I'm referring to the new commits as F'
, G'
, H'
- this is because while they will contain the same effective changes as F
, G
, and H
, they won't be the actual same commits (due to different parents + commit times).
请注意,我指的是新提交的F'
,G'
,H'
-这是因为当他们将含有相同的有效变化F
,G
以及H
,他们不会实际提交相同(由于不同的父母+提交时间)。
回答by GoZoner
Do you want the 20 commits to remain on the current branch? If not, then keep a reference to the current branch back 20 commits with:
您希望 20 个提交保留在当前分支上吗?如果没有,则保留对当前分支的引用,返回 20 个提交:
git checkout -b current_branch_save current_branch~20
Then, move the last 20 commits with rebase
然后,使用 rebase 移动最后 20 个提交
git checkout current_branch
git rebase --onto another_branch current_branch~20 current_branch
Now you've got current_branch_save with your ~100 'not quite ready' commits and current_branch on another_branch with the last 20.
现在你已经有了 current_branch_save 和你的 ~100 个“还没有准备好”的提交,而 current_branch 在 another_branch 上有最后 20 个。