Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

git - 如何取消本地git提交(How to cancel a local git commit)

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, 为我的测试行添加了一行新 '并保存了文件,然后我发出了以下命令)

 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.

(但是我从README文件中丢失了新增的“ 我的测试行 ”。)

This should not happen.

(这不应该发生。)

I need the content to be there.

(我需要内容。)

Is there a way to retain the content and cancel my local commit?

(有没有办法保留内容并取消我的本地提交?)

  ask by Amal Kumar S translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use git reset without the --hard flag:

(只需使用没有--hard标志的git reset :)

git reset HEAD~1

PS: On Unix based systems you can use HEAD^ which is equal to HEAD~1 .

(PS:在基于Unix的系统上,你可以使用等于HEAD~1 HEAD^ 。)

On Windows HEAD^ will not work because ^ signals a line continuation.

(在Windows上, HEAD^将无法工作,因为^表示行继续。)

So your command prompt will just ask you More?

(所以你的命令提示符只会问你More?)

.

(。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...