如何在不丢失更改的情况下取消提交最后一次未推送的git提交
本文翻译自:How to un-commit last un-pushed git commit without losing the changes
Is there a way to revert a commit so that my local copy keeps the changes made in that commit, but they become non-committed changes in my working copy? 有没有办法恢复提交,以便我的本地副本保留在该提交中所做的更改,但它们在我的工作副本中变为未提交的更改? Rolling back a commit takes you to the previous commit - I want to keep the changes made but I committed them to the wrong branch. 回滚提交会将您带到上一次提交 - 我想保留所做的更改,但我将它们提交到了错误的分支。
This has not been pushed, only committed. 这没有被推动,只是承诺。
#1楼
参考:https://stackoom.com/question/1LKMI/如何在不丢失更改的情况下取消提交最后一次未推送的git提交
#2楼
There are a lot of ways to do so, for example: 有很多方法可以做到这一点,例如:
in case you have not pushed the commit publicly yet: 如果您还没有公开推送提交:
git reset HEAD~1 --soft
That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. 就是这样,您的提交更改将在您的工作目录中,而LAST提交将从您当前的分支中删除。 See git reset man 见git reset man
In case you did push publicly (on a branch called 'master'): 如果你做了公开推(上一个名为“主”分支):
git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )
revert commit normally and push 正常恢复提交并推送
git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this intorduces a new coomit (say, it's hash is 86b48ba) which removes changes, introduced in the coomit in question (but those changes are still visible in the history)
git push origin master
now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with -no-commit
option: 现在,如果您希望在工作副本中进行本地更改时进行这些更改(“以便本地副本保留在该提交中进行的更改”) - 只需使用-no-commit
选项恢复还原提交:
git revert --no-commit 86b48ba (hash of the revert commit).
I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master 我精心设计了一个小例子: https : //github.com/Isantipov/git-revert/commits/master
#3楼
If you pushed the changes, you can undo
it and move the files back to stage without using another branch. 如果您按下更改,则可以undo
更改并将文件移回舞台而不使用其他分支。
git show HEAD > patch
git revert HEAD
git apply patch
It will create a patch file that contain the last branch changes. 它将创建一个包含最后一个分支更改的补丁文件。 Then it revert the changes. 然后它还原更改。 And finally, apply the patch files to the working tree. 最后,将补丁文件应用于工作树。
#4楼
For the case: "This has not been pushed, only committed ." 对于这种情况:“这没有推, 只是承诺 。” - if you use IntelliJ (or another JetBrains IDE) and you haven't pushed changes yet you can do next. - 如果您使用IntelliJ (或其他JetBrains IDE)并且尚未推送更改 ,则可以执行下一步操作。
- Go to Version control window ( Alt + 9 ) - "Log" tab. 转到版本控制窗口( Alt + 9 ) - “日志”选项卡。
- Right-click on a commit before your last one. 在最后一个之前右键单击提交。
- Reset current branch to here 将当前分支重置为此处
- pick Soft (!!!) 选择软 (!!!)
- push the Reset button in the bottom of the dialog window. 按下对话框窗口底部的Reset按钮。
Done. 完成。
This will "uncommit" your changes and return your git status to the point before your last local commit. 这将“取消提交”您的更改并将您的git状态返回到上次本地提交之前的位置。 You will not lose any changes you made. 您不会丢失所做的任何更改。
#5楼
With me mostly it happens when I push changes to the wrong branch and realize later. 对我来说,主要是当我将更改推送到错误的分支并稍后实现时。 And following works in most of the time. 以下是大多数时间的作品。
git revert commit-hash
git push
git checkout my-other-branch
git revert revert-commit-hash
git push
- revert the commit 恢复提交
- (create and) checkout other branch (创建并)结帐其他分支
- revert the revert 恢复还原
#6楼
PLease make sure to backup your changes before running these commmand in a separate folder 请务必在单独的文件夹中运行这些命令之前备份您的更改
git checkout branch_name git checkout branch_name
Checkout on your branch 在您的分支机构结账
git merge --abort git merge --abort
Abort the merge 中止合并
git status git状态
Check status of the code after aborting the merge 中止合并后检查代码的状态
git reset --hard origin/branch_name git reset --hard origin / branch_name
these command will reset your changes and align your code with the branch_name (branch) code. 这些命令将重置您的更改并将您的代码与branch_name(分支)代码对齐。