如何取消提交上次未推送的git提交而不会丢失更改 [英] How to un-commit last un-pushed git commit without losing the changes

查看:862
本文介绍了如何取消提交上次未推送的git提交而不会丢失更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以恢复提交,以便我的本地副本保留在该提交中所做的更改,但它们在我的工作副本中变成未提交的更改?回滚提交会将您带到之前的提交 - 我想保留所做的更改,但是我将它们提交给了错误的分支。



这并未推送,只提交。

解决方案

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



如果您 未公开推送提交:

  git reset HEAD〜1 --soft 

就是这样,您的提交更改将存在于您的工作目录,而最后的提交将从您当前的分支中删除。请参阅 git reset man






如果您 公开推送(在名为'master'的分支上):

<$ p $
> git checkout -b MyCommit //将你的提交保存在一个单独的分支中,以防万一(所以你不必从reflog中挖掘它,以防止你搞砸:))

正常恢复提交并按下

  git checkout master 
git revert a8172f36 #hash你想销毁的提交
#这个intorduces一个新的coomit(比如它的散列是86b48ba),它删除了引起问题的coomit (但这些变化在历史中仍然可见)
git push origin master

now if你希望在你的工作副本中进行本地修改(让你的本地副本保留在该提交中所做的修改) - 只需使用 -no-commit 选项:

  git revert --no-commit 86b48ba(回复提交的散列)。 

我制作了一个小例子: https://github.com/Isantipov/git-revert/commits/master


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.

解决方案

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. See 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:

git revert --no-commit 86b48ba (hash of the revert commit).

I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master

这篇关于如何取消提交上次未推送的git提交而不会丢失更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆