如何取消本地 git 提交

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

How to cancel a local git commit

gitgit-resetgit-commit

提问by Amal Kumar S

My issue is I have changed a file eg: README, added a new line 'this for my testing line' and saved the file, then I issued the following commands

我的问题是我更改了一个文件,例如:README,为我的测试行添加了一个新行' this'并保存了文件,然后我发出了以下命令

 git status

 # On branch master
 # Changed but not updated:
 #   (use "git add <file>..." to update what will be committed)
 #   (use "git checkout -- <file>..." to discard changes in working directory)
 #
 #  modified:   README
 #
 no changes added to commit (use "git add" and/or "git commit -a")


 git add README

 git commit -a -m 'To add new line to readme'

I didn't push the code to github, Now I want to cancel this commit.

我没有把代码推送到github,现在我想取消这个提交。

For this I used

为此,我使用了

   git reset --hard HEAD~1

But I lost the newly added line 'this for my testing line' from the README file. This should not happen. I need the content to be there. Is there a way to retain the content and cancel my local commit?

但是我从 README 文件中丢失了新添加的行“ this for my testing line”。这不应该发生。我需要内容在那里。有没有办法保留内容并取消我的本地提交?

回答by Koraktor

Just use git resetwithout the --hardflag:

只需在git reset没有--hard标志的情况下使用:

git reset HEAD~1

PS: On Unix based systems you can use HEAD^which is equal to HEAD~1. On Windows HEAD^will not work because ^signals a line continuation. So your command prompt will just ask you More?.

PS:在基于 Unix 的系统上,您可以使用HEAD^which 等于HEAD~1. 在 WindowsHEAD^上将无法工作,因为^表示行继续。所以你的命令提示符只会问你More?

回答by TpED

Use --softinstead of --hardflag:

使用--soft代替--hard标志:

git reset --soft HEAD^

回答by inostia

If you're in the middle of a commit (i.e. in your editor already), you can cancel it by deleting all lines above the first #. That will abort the commit.

如果您正在提交(即已经在您的编辑器中),您可以通过删除第一个#. 这将中止提交。

So you can delete all lines so that the commit message is empty, then save the file:

所以你可以删除所有行,使提交消息为空,然后保存文件:

It should look like this.

它应该是这样的。

You'll then get a message that says Aborting commit due to empty commit message..

然后,您会收到一条消息,内容为Aborting commit due to empty commit message.

EDIT:

编辑

You can also delete allthe lines and the result will be exactly the same.

您也可以删除所有行,结果将完全相同。

To delete all lines in vim (if that is your default editor), once you're in the editor, type ggto go to the first line, then dGto delete all lines. Finally, write and quit the file with wqand your commit will be aborted.

要删除 vim 中的所有行(如果这是您的默认编辑器),请在编辑器中输入gg转到第一行,然后dG删除所有行。最后,写入并退出文件,wq您的提交将被中止。

回答by mistdon

The first thing you should do is to determine whether you want to keep the local changes before you delete the commit message.

您应该做的第一件事是在删除提交消息之前确定是否要保留本地更改。

Use git logto show current commit messages, then find the commit_idbeforethe commit that you want to delete, not the commit you want to delete.

使用git log显示当前提交信息,然后找到了commit_id之前的承诺,你要删除,而不是要删除提交。

If you want to keep the locally changed files, and just delete commit message:

如果要保留本地更改的文件,只需删除提交消息:

git reset --soft commit_id

git reset --soft commit_id

If you want to delete all locally changed files and the commit message:

如果要删除所有本地更改的文件和提交消息:

git reset --hard commit_id

git reset --hard commit_id

That's the difference of softand hard

这就是区别

回答by Nesha Zoric

You can tell Git what to do with your index (set of files that will become the next commit) and working directory when performing git reset by using one of the parameters:

您可以使用以下参数之一告诉 Git 在执行 git reset 时如何处理索引(将成为下一次提交的文件集)和工作目录:

--soft: Only commits will be reseted, while Index and the working directory are not altered.

--soft: 只会重置提交,而不会更改索引和工作目录。

--mixed: This will reset the index to match the HEAD, while working directory will not be touched. All the changes will stay in the working directory and appear as modified.

--mixed: 这将重置索引以匹配 HEAD,而不会触及工作目录。所有更改都将保留在工作目录中并显示为已修改。

--hard: It resets everything (commits, index, working directory) to match the HEAD.

--hard:它重置所有内容(提交、索引、工作目录)以匹配 HEAD。

In your case, I would use git reset --softto keep your modified changes in Index and working directory. Be sure to check this outfor a more detailed explanation.

在您的情况下,我将使用git reset --soft在索引和工作目录中保留您修改后的更改。请务必查看此内容以获得更详细的解释。

回答by Omkar

Use below command:

使用以下命令:

$ git reset HEAD~1

After this you also able to view files what revert back like below response.

在此之后,您还可以查看恢复到以下响应的文件。

Unstaged changes after reset:
M   application/config/config.php
M   application/config/database.php