Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k points)

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'

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

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?

1 Answer

0 votes
by (27.5k points)

Use --soft instead of --hard flag:

$ git reset --soft HEAD^

If you want to know more about --soft, --mixed, and --hard you can refer to this answer

<!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}-->https://intellipaat.com/community/10440/in-plain-english-what-does-git-reset-do?show=11076#a11076

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...