撤消 Git 中已推送到远程存储库的特定提交

发布于 2024-08-23 08:26:52 字数 903 浏览 2 评论 0原文

撤消特定提交的最简单方法是什么:

  • 不在头部或 HEAD
  • 已被推送到远程。

因为如果不是最新的提交,则

git reset HEAD

不起作用。而且因为它已经被推送到了遥控器,

git rebase -i

并且

git rebase --onto

会导致遥控器出现一些问题。

更重要的是,我真的不想修改历史。如果有错误的代码,它就存在于历史中并且可以被看到。我只想将其放在工作副本中,并且我不介意反向合并提交。

换句话说,以下 svn 命令的 Git 等效项是什么

svn merge -r 303:295 http://svn.example.com/repos/calc/trunk

:通过反向合并这些修订中的所有更改作为新的提交,从 295 到 302 的所有更改。

svn merge -c -302 ^/trunk

这会撤消 302 提交,当然是通过添加另一个提交来反向合并来自相应提交的更改。

我认为这应该是 Git 中相当简单的操作,也是一个相当常见的用例。原子提交还有什么意义呢?

我们有暂存存储以及所有确保提交是完全原子的,应该您不能轻松撤消其中一个或多个原子提交吗?

What is the simplest way to undo a particular commit that is:

  • not in the head or HEAD
  • Has been pushed to the remote.

Because if it is not the latest commit,

git reset HEAD

doesn't work. And because it has been pushed to a remote,

git rebase -i

and

git rebase --onto

will cause some problem in the remotes.

More so, I don't want to modify the history really. If there was bad code, it was there in the history and can be seen. I just want it out in the working copy, and I don't mind a reverse merge commit.

In other words, what is the Git equivalent of the following svn commands:

svn merge -r 303:295 http://svn.example.com/repos/calc/trunk

which removes all changes from 295 to 302 by reverse merging all changes in those revisions, as a new commit.

svn merge -c -302 ^/trunk

which undoes the 302 commit, of course by adding another commit that reverse merges the changes from that respective commit.

I thought it should be a fairly simple operation in Git and a fairly common use case. What else is the point of atomic commits?

We have staging stashing and all to ensure the commits are perfectly atomic, shouldn't you be able to undo one or more of those atomic commits easily?

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

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

发布评论

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

评论(4

旧时浪漫 2024-08-30 08:26:53

因为它已经被推送了,所以你不应该直接操纵历史。 git revert 将使用新的提交来恢复提交中的特定更改,以便不操纵提交历史记录。

Because it has already been pushed, you shouldn't directly manipulate history. git revert will revert specific changes from a commit using a new commit, so as to not manipulate commit history.

囍孤女 2024-08-30 08:26:53

如果您要恢复的提交是合并提交(已合并),那么您应该使用 -m 1-m 2 选项,如下所示。这将使 git 知道要使用合并提交的哪个父提交。更多详细信息可以在此处找到。

  • git revert <提交>; -m 1
  • git revert <提交>; -m 2

If the commit you want to revert is a merged commit (has been merged already), then you should either -m 1 or -m 2 option as shown below. This will let git know which parent commit of the merged commit to use. More details can be found HERE.

  • git revert <commit> -m 1
  • git revert <commit> -m 2
做个ˇ局外人 2024-08-30 08:26:52

使用 git log 识别提交的哈希值,然后使用 git revert创建删除这些更改的新提交。在某种程度上,git revertgitcherry-pick 相反——后者将补丁应用到缺少补丁的分支,前者将补丁从缺少补丁的分支中删除。有它。

Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick -- the latter applies the patch to a branch that's missing it, the former removes it from a branch that has it.

莫相离 2024-08-30 08:26:52

我不喜欢 git revert 的自动提交功能,因此这可能对某些人有帮助。

如果你只想修改文件而不是自动提交,你可以使用--no-commit

% git revert --no-commit <commit hash>

,它与-n相同

% git revert -n <commit hash>

I don't like the auto-commit that git revert does, so this might be helpful for some.

If you just want the modified files not the auto-commit, you can use --no-commit

% git revert --no-commit <commit hash>

which is the same as the -n

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