To undo a
Git commit that was not pushed, you are given a few major options:
commitbut keep all changes
lose all changes
Method 1: Undo commit and keep all files staged
In case you just want to
commit and change nothing more, you can use
git reset --soft HEAD~;
This is most often used to make a few changes to your latest commit and/or fix your commit message. Leaves working tree as it was before
soft does not touch the index file or the working tree at all (but resets the
head to the previous
commit). This leaves all your changed files
Changes to be committed, as
git status would put it.
Method 2: Undo commit and unstage all files
In case you want to
undo the last
unstage all the files you can use the following
git reset HEAD~;
git reset --mixed HEAD~;
index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
Method 3: Undo the commit and completely remove all changes
The following method will
revert all changes so that your state is exactly as it was before you started making changes.
git reset --hard HEAD~;
hard resets the index and working tree. Any
changes to tracked files in the working tree since the previous commit
Note: In case you just want to rewrite the
commit message, you could use git –amend instead.
This post is also available in: Greek
git reset --hard HEAD~;
Can be used to abort a merge that is in progress.
Hey George, this was a great post I appreciate you sharing. I had one follow up question which I was not 100% on. When using method 2 – undoing the commit but “unstaging” all files. Would you be able to help me further understand what it means to unstage files in this context? I am a a bit new and am having trouble wrapping my head around some of these concepts.
In the process of creating a commit, you normally specify the files to be added to the commit by using the
git addcommand. All files that are added to a commit before executing the
git commitcommand are called staged.
When you use method 1, it will undo the commit but keep the files staged, this way you can re-create your commit in as few steps as possible (it is possible to just execute
git commitwith a new message immediately and create a new commit).
Using method 2 on the other hand, not only will it revert the commit but it will also clear the stage. This means that if you want to create a new commit, then you will have to specify from scratch which files should be added to the commit using
Thank you, you saved my life
Thank you so much, I was looking everywhere on how to undo last commit so I can change the commit message and recommit. Yours was the only answer that worked. Brilliant.
If only the commit message need to be changed, then simple command is
git commit –amend -m “new message”
Pingback: Eliminar una confirmación de git no enviada - git en Español