Git undo last commit
How to undo the last commit in git, keeping your changes or discarding them
This will be a very short post, because it’s meant primarily as a way for me to remember a git command which I seldom use, but when I do, I never remember the exact syntax.
As they say:
If you want to remember somethinng, write it down
Undo a commit
In this case you will have to use the reset command and will have to specify the revision you want to rewind to.
Keeping your changes
To preserve the changes, use the --soft flag which will preserve your revisions. At this point you will be able to make any changes and re-commit them once you are done with your work.
| |
Dropping your changes
If instead you want to discard all the changes, you will have to use the --hard flag.
| |
The HEAD~1 syntax is a short form to specify how far back we want to rewind, ~1 specifies the last revision. Of course you can do HEAD~3 which would rewind the last 3 commits.
Or if you want to specify a specific revision you can simply to the following
| |
Use with caution since you will lose the commits you have discarded.
Push the changes
To push the changes to your remote repo, you will have to use the -f option to force push, this will in effect strip the remote commits, so please make sure you exercise caution when doing so.
| |