简体   繁体   English

如何在最后一次提交之前取消提交

[英]How to nuke a commit before last commit

Suppose I have 2 branches master and dev . 假设我有2个分支masterdev

The dev branch is ahead of master by 2 commits, and neither of those commits is merged or pushed to any remote . dev分支比master领先2次提交,并且这些提交均未合并或推送到任何remote

The dev branch looks like this: dev分支如下所示:

dev A - B - C - D - E

Whereas, A is oldest, and E newest commit. 而A是最早的,而E是最新的。

Id like to get to this state: 我想进入这种状态:

dev A - B - C - E

Basically I want to nuke the commit before the last commit, as if it never happened. 基本上,我想在最后一次提交之前取消提交,好像从未发生过一样。 There should be no conflicts either (E does not depend on D's changes). 也不应该有冲突(E不依赖于D的更改)。

Could I use interactive rebase? 我可以使用交互式变基吗?

I would use the itneractive form of rebase. 我会使用变基的迭代形式。 While on dev (you need to have clean working copy): dev (您需要具有干净的工作副本):

git rebase -i HEAD~~

In your favourite text editor, you get a list of last 2 commits. 在您喜欢的文本编辑器中,您将获得最近2次提交的列表。 Just comment/delete the line for the unwanted commit. 只需注释/删除不需要的提交行。

If HEAD depends on changes introduced by HEAD~ , you could get merge conflicts and after you resolve them, you will loose your previous history (still available through the reflog , of course). 如果HEAD依赖于引入的变化HEAD~你可以得到合并冲突,你解决这些问题后,你将失去以前的历史记录(仍然可以通过reflog ,当然)。 I usually prefer this method. 我通常更喜欢这种方法。

If you want to conserve your previous history (and the unneeded commit), you should do 如果您想保存以前的历史记录(和不需要的提交),则应该这样做

git checkout HEAD~~         # go to last good commit
git checkout -b new_branch  # create a new branch and forget about the original
git cherry_pick dev         # copy the commit on top of `dev` to `new_branch`

You will still get a conflict, but you won't be modifying the history of dev . 您仍然会遇到冲突,但是您不会修改dev的历史。

On the other hand, if the unwanted commit was on master and you didn't want to break anybody's build, you would do git revert HEAD~ 另一方面,如果不需要的提交在master并且您不想破坏任何人的构建,则可以执行git revert HEAD~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM