My issue is I have changed a file e.g.: 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?

git revert, which creates a new commit with the reverse diff of the reverted commit. Resetting simply points your current branch to a different commit, in this case, the one before the commit you want to "forget".git-commitcan abort if you leave the message blank, so if you haven't actually finished the commit that could be helpful.