如何“重新打开”最后一次 git 提交?

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

How to "reopen" the last git commit?

git

提问by Matthieu Napoli

I was working on a branch and needed to get back on master for a quick fix so I committed my work in progress.

我在一个分支上工作,需要回到 master 上进行快速修复,所以我承诺了我正在进行的工作。

Now I am back in my branch and I would like to get back to the state where I haven't committed but all my changes are applied and ready to be committed (i.e. cancel the previous commit but my changes are not lost and they are shown in git status).

现在我回到了我的分支,我想回到我尚未提交的状态,但我的所有更改都已应用并准备好提交(即取消之前的提交但我的更改没有丢失并且它们会显示)在git status)。

Amending the previous commit is not good enough for me as I want to see the changed files and lines (it makes it easier to work on my change).

修改之前的提交对我来说还不够好,因为我想查看更改后的文件和行(这样可以更轻松地处理我的更改)。

I know git stash would have been a better solution but it's too late. I also tend to mix up (or loose) my stashed commits as they are not linked to a branch.

我知道 git stash 会是一个更好的解决方案,但为时已晚。我也倾向于混淆(或松散)我的隐藏提交,因为它们没有链接到分支。

回答by Willem Van Onsem

There are basically two ways to solve this:

基本上有两种方法可以解决这个问题:

  1. Do a reset (on the masterbranch, or whatever branch the false commit is):

    git reset --soft HEAD~1
    

    The HEAD~1is a relative address as explained here.

  2. Keep working and perform an amendment:

    git commit --amend
    

    In that case you pretend nothing happened, work further on your branch and call the command when you were planning to do a real commit. You kind of overwritethe last commit with all changes and (optionally) a new message.

  1. 进行重置(在master分支上,或错误提交所在的任何分支上):

    git reset --soft HEAD~1
    

    HEAD~1所解释的是一个相对地址在这里

  2. 继续工作并进行修改:

    git commit --amend
    

    在这种情况下,你假装什么也没发生,在你的分支上进一步工作并在你计划进行真正的提交时调用命令。您可以使用所有更改和(可选)新消息覆盖上次提交。

And pro forma: it is indeed - as you mention yourself - better to perform a git stash, only saying for people that see this answer by accident and think the above is common practice(it is not).

并且备考:确实 - 正如你自己提到的 - 更好地执行 a git stash,只对那些偶然看到这个答案并认为上述是常见做法的人说(事实并非如此)。

回答by Christopher Wells

From my understanding, you want to undo the latest commit to your branch while keeping all of the changes from the commit intact and staged.

根据我的理解,您希望撤消对分支的最新提交,同时保持提交中的所有更改完整无缺并暂存。

You may want to try using the git reset command as listed below:

您可能想尝试使用下面列出的 git reset 命令:

git reset --soft HEAD^

That should take your latest commit and undo it, while keeping all of the changes staged.

这应该采用您最新的提交并撤消它,同时保持所有更改的上演。

回答by Maxim Leonovich

You should use git reset HEAD~1(without --hardoption)

您应该使用git reset HEAD~1(不带--hard选项)

It will do exactly what you want and HEAD~1 means here that you'd like to reset one commit, counting from the head (top) of the history.

它将完全按照您的意愿执行操作, HEAD~1 在这里表示您想重置一次提交,从历史记录的头部(顶部)开始计数。

This will not touch any files on the file system, so git will now see your edited files as changed but not committed.

这不会触及文件系统上的任何文件,因此 git 现在将看到您编辑的文件已更改但未提交。