GIT 撤消不是最近的提交,重新调整所有提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1969740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:34:45  来源:igfitidea点击:

GIT undo a commit that isn't the most recent, rebase all commits since

gitgit-rebase

提问by rjmunro

Earlier on, I committed some code which at the time seemed like a good idea, but in fact it made things worse. I'd like to remove the change from the history as it is not helpful, and rebase all commits since - they should all merge without issues.

早些时候,我提交了一些在当时看起来是个好主意的代码,但实际上它让事情变得更糟。我想从历史记录中删除更改,因为它没有帮助,并且重新设置所有提交,因为它们都应该合并而不会出现问题。

I tried creating a new branch (called newMaster) immediately before the bad commit (with gitx), checking it out and running:

我尝试newMaster在错误提交(使用 gitx)之前立即创建一个新分支(称为),检查它并运行:

git rebase -i master

Which AFAICS should have given me a list of the commits on master and the option to merge or not merge them, but instead it said noop, and fast forwarded newMaster to be equal to master.

哪个 AFAICS 应该给我一个 master 上的提交列表以及合并或不合并它们的选项,而是说 noop,并快速转发 newMaster 以等于 master。

What is the easiest way to do what I need?

做我需要的最简单的方法是什么?

采纳答案by Brian Campbell

git rebase -iis the right command, but you want to do it from the branch with the latest changes, and pass in the revision that is the "base" of the rebase operation (the revision immediately before the bad commit). So, if you have created a branch last-good-committhat points to the last good commit, you would want to run the following while on master:

git rebase -i是正确的命令,但您希望从具有最新更改的分支执行此操作,并传入作为 rebase 操作的“基础”的修订(错误提交之前的修订)。因此,如果您创建了一个last-good-commit指向最后一个好的提交的分支,您可能希望在 on 时运行以下命令master

git rebase -i last-good-commit

回答by CB Bailey

The easiest way to do what you want is to stay on (or re-checkout) the branch that you want to edit and run something like this.

做您想做的事情的最简单方法是留在(或重新结帐)您要编辑的分支上并运行类似的操作。

git rebase --onto <sha1-of-bad-commit>^ <sha1-of-bad-commit>

This will rebase everything since the bad commit onto the bad commit's parent, effectively removing the bad commiit from your current branch's history. Of course you will need to resolve any conflicts if they occur.

这会将错误提交后的所有内容重新设置到错误提交的父级,有效地从当前分支的历史记录中删除错误提交。当然,如果发生任何冲突,您将需要解决它们。

回答by danza

Using rebase, I was not able to push the changes to the remote, and my rebase was canceled at each pull.

使用rebase,我无法将更改推送到遥控器,并且每次拉动时我的变基都被取消。

I succeeded in reverting a precise commit (not the last one) with, simply:

我成功地恢复了一个精确的提交(不是最后一个),只需:

git revert <sha-of-bad-commit>

And then I was also able to push on the remote

然后我还可以推动遥控器