如何在 git push 之后取消 git commit?

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

How can I un-do a git commit AFTER a git push?

git

提问by michael

From here http://blog.prabir.me/post/Undo-in-Git.aspx, it said

从这里http://blog.prabir.me/post/Undo-in-Git.aspx,它说

This undo's your commit and also resets your working tree to the last commit.

1 git reset --hard HEAD^

But how can I un-do my last commit AFTER I did a 'git push'?

但是,在我执行了 'git push' 之后,我怎样才能取消我的最后一次提交呢?

Thank you.

谢谢你。

When I do 'git log', I get as my top commit

当我做'git log'时,我得到了我的最高提交

commit 29fc764693a5933a379169e22891e4b0d3d2426f
Merge: 002db49 cfb1d8f

How can I 'git revert' that change?

我怎样才能“git恢复”这种变化?

I get this

我明白了

$ git revert HEAD 
fatal: Commit 29fc764693a5933a379169e22891e4b0d3d2426f is a merge but no -m option was given.

回答by Grizzly

you can use git revert HEAD, which generates a new commit, which will undo the changes in your previous commit. Look here. Note however that both the commit and the revert commit will show up in the history.

您可以使用git revert HEAD,它会生成一个新提交,这将撤消之前提交中的更改。看这里。但是请注意,提交和还原提交都将显示在历史记录中。

Edit: As KingChrunch mentioned, when reverting a merge commit, you need to specify which parent you want to revert to, so add -m <parent>. Of course simply following the link I have posted would have told you so.

编辑:正如 KingCrunch 提到的,在还原合并提交时,您需要指定要还原到哪个父级,因此添加-m <parent>. 当然,只要按照我发布的链接就可以告诉你。

You can't (well actually shouldn't) modify the history of a shared repository. If you where inclined to modify the history (please don't), you can use git resetwith git push --force, or git rebase.

您不能(实际上不应该)修改共享存储库的历史记录。如果您倾向于修改历史记录(请不要),您可以使用git resetwith git push --force, 或git rebase

回答by Fred Foo

You should git revertthe commit.

你应该git revert提交。

You can also git push --forcethe branch after the git reset, but if anyone pulled your branch before that, your histories will have diverged, so you really shouldn't do this.

您也可以git push --force在 之后的分支git reset,但是如果有人在此之前拉了您的分支,那么您的历史记录就会发生分歧,所以您真的不应该这样做。

回答by Manda QoP

Just found an even easier method if you're using GitLab and a code review/merge request step. Found this after I accidentally included an unnecessary file in a commit & push. You can also use this method to make edits and recommit them to your branch before merging.

如果您正在使用 GitLab 和代码/合并请求步骤,那么刚刚找到了一种更简单的方法。在我不小心在提交和推送中包含一个不必要的文件后发现了这个。您还可以使用此方法进行编辑并在合并之前将它们重新提交到您的分支。

Our process: After coding, we commit, fetch from upstream, rebase, push to GitLab. This creates a branch on GitLab, we then create a merge request for that branch. This is to ensure all code gets code reviewed by a peer before going in the main branch. In this case it was an extremely useful step as my peer review colleague spotted my gaff.

我们的过程:编码后,我们提交,从上游获取,rebase,推送到 GitLab。这会在 GitLab 上创建一个分支,然后我们为该分支创建一个合并请求。这是为了确保所有代码在进入主分支之前都经过同行。在这种情况下,这是一个非常有用的步骤,因为我的同行评审同事发现了我的错误。

Note: This is only an option BEFORE a merge request is accepted.

注意:这只是在接受合并请求之前的一个选项。

To remove a file from a commit after pushing to GitLab and BEFORE merging:

在推送到 GitLab 之后和合并之前从提交中删除文件:

  1. Open the GitLab merge request
  2. Select the 'Changes' tab
  3. Find the file that is unwanted in the commit
  4. Click the 'View file' button for the file
  5. Click the 'Delete' button
  6. Enter the commit information and commit the change
  1. 打开 GitLab 合并请求
  2. 选择“更改”选项卡
  3. 找到提交中不需要的文件
  4. 单击文件的“查看文件”按钮
  5. 单击“删除”按钮
  6. 输入提交信息并提交更改

Your merge request has now been fixed and the unwanted file removed from your commit. You can now merge (if the rest pass code review).

您的合并请求现已得到修复,并且从您的提交中删除了不需要的文件。您现在可以合并(如果其余通过代码)。

You can also edit and recommit files this way. It's an easy way to make quick fixes without needing to open Eclipse/IntelliJ (whatever other tool you use) and checking out the branch.

您还可以通过这种方式编辑和重新提交文件。这是一种快速修复的简单方法,无需打开 Eclipse/IntelliJ(无论您使用什么其他工具)并检查分支。

This just saved me hours of pain avoiding dealing with removing a file from a pushed branch in Eclipse.

这只是为我节省了数小时的痛苦,避免了从 Eclipse 中的推送分支中删除文件的处理。