How to cancel a local git commit?

GitGit ResetGit Commit

Git Problem Overview


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 Solutions


Solution 1 - Git

Just use git reset without the --hard flag:

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

Solution 2 - Git

Use --soft instead of --hard flag:

git reset --soft HEAD^

It will remove the last local (unpushed) commit, but will keep changes you have done.

Solution 3 - Git

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

Solution 4 - Git

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 log to show current commit messages, then find the commit_id before the commit that you want to delete, not the commit you want to delete.

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

git reset --soft commit_id

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

git reset --hard commit_id

That's the difference of soft and hard

Solution 5 - Git

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:

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

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

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

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

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmal Kumar SView Question on Stackoverflow
Solution 1 - GitKoraktorView Answer on Stackoverflow
Solution 2 - GitTpEDView Answer on Stackoverflow
Solution 3 - GitinostiaView Answer on Stackoverflow
Solution 4 - GitmistdonView Answer on Stackoverflow
Solution 5 - GitNesha ZoricView Answer on Stackoverflow
Solution 6 - GitOmkarView Answer on Stackoverflow