如何取消本地git commit [英] How to cancel a local git commit

查看:844
本文介绍了如何取消本地git commit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 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'

我没有将代码推送到github,现在我想取消此提交.

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

为此,我使用了

   git reset --hard HEAD~1

但是我从README文件中丢失了新添加的行"这是我的测试行". 这不应该发生.我需要那里的内容. 是否可以保留内容并取消本地提交?

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?

推荐答案

只需使用不带--hard标志的git reset:

git reset HEAD~1

PS:在基于Unix的系统上,您可以使用HEAD^,它等于HEAD~1.在Windows上,HEAD^将不起作用,因为^表示行继续.因此,您的命令提示符将只问您More?.

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?.

这篇关于如何取消本地git commit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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