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

发布于 2024-08-15 17:43:03 字数 331 浏览 6 评论 0原文

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

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

git rebase -i master

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

完成我需要的事情的最简单方法是什么?

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:

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.

What is the easiest way to do what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命比纸薄 2024-08-22 17:43:03

做你想做的事情的最简单的方法是留在(或重新签出)你想要编辑的分支并运行类似的东西。

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

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

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.

清音悠歌 2024-08-22 17:43:03

git rebase -i 是正确的命令,但您想从具有最新更改的分支执行此操作,并传入作为 rebase 操作的“基础”的修订版(紧邻之前的修订版)错误的提交)。因此,如果您创建了一个指向最后一次良好提交的分支 last-good-commit,则您需要在 master 上运行以下命令:

git rebase -i last-good-commit

git rebase -i is 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-commit that points to the last good commit, you would want to run the following while on master:

git rebase -i last-good-commit
岁月无声 2024-08-22 17:43:03

使用rebase,我无法将更改推送到远程,并且我的rebase在每次拉取时都被取消。

我成功地恢复了精确的提交(不是最后一次),简单地说:

git revert <sha-of-bad-commit>

然后我还能够在远程上推送

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文