git 在某个提交后删除分支中的所有提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13772550/
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
Deleting all commits in a branch after certain commit
提问by Zaki Aziz
I'm having a difficult time grasping how I can use rebase/revert to accomplish this.
我很难掌握如何使用 rebase/revert 来实现这一点。
I was working on my master
branch and after a certain commit my software stopped working. I did not want to loose the changes made at the time and I was pressured on time to reach a milestone so I went back a few commits with git checkout
and created a new branch called working
and started pushing all my changes there. Later on I realized that these changes (made on the master
branch) were not needed. Now I want to go back to my master branch and delete all the commits after the commit I used to create my working
branch and then merge my working
branch to master
branch.
我在我的master
分支上工作,在某个提交后,我的软件停止工作。我不想放弃当时所做的更改,而且我迫于压力要按时达到里程碑,所以我返回了一些提交git checkout
并创建了一个名为的新分支,working
并开始将我的所有更改推送到那里。后来我意识到master
不需要这些更改(在分支上进行)。现在我想回到我的主分支并删除我用来创建working
分支的提交之后的所有提交,然后将我的working
分支合并到master
分支。
I've created an image with an online photo editor to try and explain what I'm trying to do. I hope it helps:
我已经使用在线照片编辑器创建了一个图像来尝试解释我正在尝试做什么。我希望它有帮助:
I want to keep everything after 5cb967f. get rid of everything between 5cb967f and a0c1de2 (not including those)
我想保留 5cb967f 之后的所有内容。摆脱 5cb967f 和 a0c1de2 之间的所有内容(不包括那些)
采纳答案by djs
You have two options:
您有两个选择:
Rewrite history (destructive)
重写历史(破坏性)
You can use git-rebase
to rewrite history, ommiting these commits. You can run an interactive rebase. From your description, I'm not certain exactly what you have in master
and working
, but I am assuming all the history (wanted and unwanted) is present there.
您可以使用git-rebase
重写历史记录,忽略这些提交。您可以运行交互式变基。根据您的描述,我不确定您在master
和 中的确切内容working
,但我假设所有历史记录(想要的和不想要的)都在那里。
git checkout master
git rebase -i a0c1de2
At this point, your $EDITOR
will pop up with a list of commits from a0c1de2
to the HEAD of master
. You can remove the lines corresponding to c460070..a3cb10e
to delete them from history.
在这一点上,你$EDITOR
会弹出从提交列表a0c1de2
来的HEAD master
。您可以删除对应的行c460070..a3cb10e
以将其从历史记录中删除。
Your history will be rewritten locally. If you attempt to push this change, it will be a non fast-forward update.
您的历史将在本地改写。如果您尝试推送此更改,它将是非快进更新。
Revert, preserving history (non-destructive)
还原,保留历史(非破坏性)
IF you prefer to keep the history, you can revert a sequence of commits:
如果您更喜欢保留历史记录,则可以还原一系列提交:
git checkout master
git revert c460070..a3cb10e
This will create 7 new commits, each reverting the changes in these unwanted commits, in order.
这将创建 7 个新提交,每个提交按顺序恢复这些不需要的提交中的更改。
回答by jthill
Something about the history graphic is screwy. Going by the text markup in it, what you want is simply
关于历史图形的某些东西很奇怪。通过其中的文本标记,您想要的只是
$ git branch -f master working
because there weren't any post-working
-base commits on master that you want to keep.
因为working
在 master 上没有任何 post-- base 提交要保留。