如何在不丢失更改的情况下取消提交最后一个未推送的 git commit

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

How to un-commit last un-pushed git commit without losing the changes

git

提问by Mr. Boy

Is there a way to revert a commit so that my local copy keepsthe 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.

这还没有被推动,只有承诺。

回答by Isantipov

There are a lot of ways to do so, for example:

有很多方法可以做到这一点,例如:

in case you have notpushed 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. See git reset man

就是这样,您的提交更改将在您的工作目录中,而 LAST 提交将从您当前的分支中删除。参见git reset man



In case you didpush publicly (on a branch called 'master'):

如果您确实公开推送(在名为“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 introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit 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-commitoption:

现在,如果您想在工作副本中进行本地更改时进行这些更改(“以便您的本地副本保留在该提交中所做的更改”) - 只需使用--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

回答by Aminadav Glickshtein

If you pushed the changes, you can undoit 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.

它将创建一个包含最后分支更改的补丁文件。然后它恢复更改。最后,将补丁文件应用到工作树。

回答by Eduard Streltsov

For the case: "This has not been pushed, only committed." - if you use IntelliJ(or another JetBrains IDE) and you haven't pushed changes yetyou can do next.

对于案例:“这还没有被推送,只有被提交。” - 如果您使用IntelliJ(或其他 JetBrains IDE)并且您还没有推送更改,您可以执行下一步操作。

  1. Go to Version control window (Alt + 9/Command + 9) - "Log" tab.
  2. Right-click on a commit before your last one.
  3. Reset current branch to here
  4. pick Soft(!!!)
  5. push the Reset button in the bottom of the dialog window.
  1. 转到版本控制窗口(Alt + 9/Command + 9)-“日志”选项卡。
  2. 在最后一次提交之前右键单击提交。
  3. 将当前分支重置到这里
  4. 选择(!!!)
  5. 按下对话窗口底部的重置按钮。

Done.

完毕。

This will "uncommit" your changes and return your git status to the point before your last local commit. You will not lose any changes you made.

这将“取消提交”您的更改并将您的 git 状态返回到您上次本地提交之前的点。您不会丢失所做的任何更改。

回答by Mubashar

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
  1. revert the commit
  2. (create and) checkout other branch
  3. revert the revert
  1. 还原提交
  2. (创建和)结帐其他分支
  3. 还原还原

回答by Mohit Singh

PLease make sure to backup your changes before running these commmand in a separate folder

请确保在单独的文件夹中运行这些命令之前备份您的更改

git checkout branch_name

git checkout 分支名称

Checkout on your branch

在您的分行结帐

git merge --abort

git merge --abort

Abort the merge

中止合并

git status

状态

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 (分支)代码对齐。