Git:在“顶部”移动提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14507510/
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: move a commit "on top"
提问by Adrian Panasiuk
Let's say in master
I have a feature disabled.
I work on that feature on branch feature
, so I have a special commit $
there that just enables that feature.
Now I want to merge the changes I did in feature
into master
, but keep the enabling commit out. So it's like
假设master
我禁用了一个功能。我在 branch 上处理该功能feature
,所以我在$
那里有一个特殊的提交,它只启用了该功能。现在我想将我所做的更改合并feature
到 中master
,但保留启用提交。所以就像
main: A--B--X--Y
feature: A--B--$--C--D
So let's say I want to do it, by moving the $
commit on top of feature:
因此,假设我想通过将$
提交移到功能之上来做到这一点:
new feature: A--B--C--D--$
How would I go about doing that?
我该怎么做?
回答by Carl Norum
git rebase -i B
, and then move $
to the end of the list that shows up in your editor. It will start out as the first line in the file that opens. You could also just delete that line entirely, which will just drop that commit out of your branch's history.
git rebase -i B
,然后移至$
编辑器中显示的列表末尾。它将从打开的文件中的第一行开始。您也可以完全删除该行,这只会将该提交从分支的历史记录中删除。
回答by Schleis
If you want to keep you commits in the same order on feature
you should make a new branch and perform the following. Otherwise just do this on feature
如果你想让你的提交保持相同的顺序,feature
你应该创建一个新分支并执行以下操作。否则就这样做feature
git rebase -i <sha for commit B>
Move commit $ to the bottom of the list
将提交 $ 移动到列表底部
git checkout master
git rebase feature <or the other branch name>
It wasn't clear to me on the question but if you didn't want $ at all, rather than moving it delete it after git rebase -i
Though you will want to do this on a new branch so that you don't lose it. As you are changing history.
我不清楚这个问题,但如果你根本不想要 $,而不是移动它,而是在之后删除它,git rebase -i
尽管你会想在一个新分支上这样做,这样你就不会丢失它。正如你正在改变历史。
This also assumes that the branch feature
hasn't been pushed to a remote as rewriting history is bad.
这也假设分支feature
没有被推送到远程,因为重写历史很糟糕。