To undo a Git commit that was not pushed, you are given a few major options:
Method 1:
Undo the commit but keep all changes staged
git reset --soft HEAD~;
Method 2:
Undo the commit and unstage the changes
git reset HEAD~;
or 1 git reset --mixed HEAD~;
Method 3:
Undo the commit and lose all changes
git reset --hard HEAD~;
Method 4:
In case you just want to rewrite the commit message, you could use:
git –amend instead.
eg: git commit --amend -m "an updated commit message"
I will add following more helpful commands to check commit history, go back to a commit, and remove commits etc:
Free Additional Tip:
To Undo a git add i.e. remove files staged for a git commit
1. git reset filename.txt OR
2. git reset *
After this you can add again the required files
Cheers :)
-Anwar Jamal Faiz
Method 1:
Undo the commit but keep all changes staged
git reset --soft HEAD~;
Method 2:
Undo the commit and unstage the changes
git reset HEAD~;
or 1 git reset --mixed HEAD~;
Method 3:
Undo the commit and lose all changes
git reset --hard HEAD~;
Method 4:
In case you just want to rewrite the commit message, you could use:
git –amend instead.
eg: git commit --amend -m "an updated commit message"
However if you only want to undo Last Git Commit
Then easiest way to undo the last Git commit is
$ git reset --soft HEAD~1
I will add following more helpful commands to check commit history, go back to a commit, and remove commits etc:
- git log
- git log --oneline to simplify the output:
- To test a specific commit: git checkout
. You will be in 'detached HEAD' state. You can look around, make experimental changes and commit them, even create a new branch from here using git checkout -b new_branch_name - To fix the detached head issue: git checkout
- Undo this commit: git revert
. This will create another commit operation in github
Free Additional Tip:
To Undo a git add i.e. remove files staged for a git commit
1. git reset filename.txt OR
2. git reset *
After this you can add again the required files
Cheers :)
-Anwar Jamal Faiz
Read some of my other posts related to Git tricks below :