简体   繁体   English

推送失败后如何撤消提交?

[英]How to undo a commit after a failed push?

I was going to test something in dev branch (just a version number 0.1 --> 0.2), so I committed the change but I don't have the permission to push it to dev directly,then I created a feature branch and I found the number is already 0.2, then I changed it to 0.3 and pushed this branch, then created a merge request to merge it into dev.我打算在 dev 分支中测试一些东西(只是版本号 0.1 --> 0.2),所以我提交了更改,但我没有权限将它直接推送到 dev,然后我创建了一个功能分支,我发现这个数字已经是0.2了,然后我把它改成0.3并推送了这个分支,然后创建了一个合并请求将它合并到dev中。

I'm new to git, just wondering how can I prevent this kind of messy in the future?我是 git 的新手,只是想知道以后如何防止这种混乱? How can I revert my commit after the push being rejected?推送被拒绝后如何恢复我的提交?

You could've solved it by reverting the last commit using the command您可以通过使用命令恢复最后一次提交来解决它

git reset HEAD~

which would undo your last commit and get your changes of the last commit back to uncommitted state.这将撤消您的最后一次提交并使您对最后一次提交的更改恢复到未提交状态。 You can then checkout a feature branch, make the commit and push the feature branch to your remote repo.然后,您可以检出功能分支,进行提交并将功能分支推送到您的远程存储库。

You can simply drop the commit from your Git history.您可以简单地从 Git 历史记录中删除提交。

If the commit hasn't been pushed, the following steps can work -如果尚未推送提交,则可以执行以下步骤 -

  1. Get on the commit you wanna remove.继续你想要删除的提交。

  2. Run git rebase -i HEAD~2运行git rebase -i HEAD~2

  3. Before the commit entry in the file that opens up, change pick to drop .在打开的文件中的提交条目之前,将pick更改为drop

  4. Exit the editor.退出编辑器。 You should now see the last commit gone.您现在应该看到最后一次提交消失了。

If you already had pushed the unwanted commit, you'd need to force push to remote.如果您已经推送了不需要的提交,则需要强制推送到远程。 But, force push is only advisable for the feature branch - never force push to the master!但是,强制推送仅适用于功能分支 - 永远不要强制推送到主人!

Similar to ros.net 's answer, I was able to do this with the GUI app "Git Extensions", by类似于ros.net的回答,我能够通过 GUI 应用程序“Git Extensions”做到这一点

  1. right-clicking on the commit I wanted to reset/rebase back to,右键单击我想重置/变基的提交,
  2. choosing option " Rebase current branch on > " with sub-option " selected commit interactively... "选择选项“ Rebase current branch on > ”和子选项“ selected commit interactively...

(click to see image, as my rep is to low to embed them) (点击查看图片,因为我的代表太低无法嵌入它们)
right-click menu for undoing a commit with an interactive rebase用于使用交互式 rebase 撤消提交的右键单击菜单

This opens up an editor window for the rebasing git command, which lets you choose how handle removing the commits above the rebase commit, and also has an glossary explaining what each command does.这会为 rebase git 命令打开一个编辑器 window,它允许您选择如何处理删除 rebase 提交之上的提交,并且还有一个词汇表解释每个命令的作用。

(click to see image, as my rep is to low to embed them) (点击查看图片,因为我的代表太低无法嵌入它们)
Initial editor window初审 window

For me, I wanted to undo a commit which added several new files, which I already had backups of in a external folder.对我来说,我想撤消添加了几个新文件的提交,我已经在外部文件夹中备份了这些文件。 So I changed 'pick' to 'drop', like so:所以我把'pick'改成了'drop',像这样:

(click to see image, as my rep is to low to embed them) (点击查看图片,因为我的代表太低无法嵌入它们)
3. pop-up editor window 3.弹出编辑器window

  1. Then I clicked the 'save' button and closed the editor window, and click 'OK' on the command pop-up window, and it was complete.然后我点击‘保存’按钮关闭编辑器window,在弹出的命令window点击‘确定’,就完成了。

My un-pushed commits are now gone and my local repo is now reset back to the same commit Head as the Origin repo.我未推送的提交现在消失了,我的本地仓库现在重置回与 Origin 仓库相同的提交头。 My staging and working directory were also emptied.我的暂存目录和工作目录也被清空了。 But thankfully I had backed up my new files and could re-commit them to a separate branch that I do have push permissions on.但值得庆幸的是,我已经备份了我的新文件,并且可以将它们重新提交到我确实拥有推送权限的单独分支。

Further documentation on rebasing with Git Extensions (a free git gui app) can be found here: Git Extensions - How To: Squash and Rebase your changes有关使用 Git Extensions(免费的 git gui 应用程序)进行变基的更多文档可以在此处找到: Git Extensions - How To: Squash and Rebase your changes

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

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