如何创建一个新分支来清理另一个分支的提交?
我有两个分支,master
和 feature
。
我完成了 feature
的工作,因此这个分支有很多提交,但我不想按原样发布它。我想创建一个名为 feature_clean
的分支,其中包含来自 feature
的所有修改,但具有更好的提交。
我尝试了以下操作:
git checkout -b feature_clean master
git checkout feature
git rebase --interactive feature_clean
# reorganize commits etc, save and close editor
,这正确创建了 feature_clean
但它也修改了 feature
。事实上,两个分支都是平等的。
我做错了什么?我想暂时保留 feature
(在 feature_clean
经过适当测试和批准后,我将删除它)。
I have two branches, master
and feature
.
I finished working on feature
, so this branch have a lot of commits, but I don't want to publish it as is. I want to create a branch called feature_clean
with all the modifications from feature
but with better commits.
I tried the following:
git checkout -b feature_clean master
git checkout feature
git rebase --interactive feature_clean
# reorganize commits etc, save and close editor
, and this created feature_clean
correctly BUT it also modified feature
. In fact, both branches were equal.
What did I do wrong? I want to keep feature
as is for now (I'll delete it later, after feature_clean
is appropriately tested and approved).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 rebase 命令错误;)你告诉 git 在
feature_clean
之上重新设置当前分支(HEAD
,在这种情况下为feature
) 。我认为您真正想要做的是:即在
master
上重放来自feature_clean
的提交。feature
仍将指向旧的提交。git rebase master
是git rebase --onto master master HEAD
的简写:获取master
和HEAD
之间的所有提交(可以从HEAD
访问,但不能从master
访问)并将它们粘贴到master
上you have your rebase command wrong ;) you are telling git to rebase the current branch (
HEAD
, in that casefeature
) on top offeature_clean
. I think what you actually want to do is:i.e. replay commits from
feature_clean
onmaster
.feature
will still point to the old commits.git rebase master
is shorthand forgit rebase --onto master master HEAD
: take all commits betweenmaster
andHEAD
(reachable fromHEAD
, but not frommaster
) and stick them ontomaster
检查您的功能分支
git checkout 功能
创建新的 feature_clean 分支
git checkout -b feature_clean
将该分支重新设置为与 master 分离的位置
git rebase -i --onto shaOfSplit shaOfSplit feature_clean
进行交互式变基
在变基结束时,您将从同一点断开两个分支。
feature_clean
将是feature
的重新基础版本Checkout your feature branch
git checkout feature
Create your new feature_clean branch
git checkout -b feature_clean
Rebase that branch onto where you split off of master
git rebase -i --onto shaOfSplit shaOfSplit feature_clean
Do your interactive rebase
At the end of your rebase you will have two branches broken off from the same point.
feature_clean
will be a rebased version offeature