How to revert latest commits in GIT?
While we hopefully consider all the “commit” best practices I talked about in a previous post, we happen to screw up (a lot), and commit…
How to revert latest commits in GIT?
How to undo my latest commits in GIT?
While we hopefully consider all the “commit” best practices I talked about in a previous post, we happen to screw up (a lot), and commit stupid stuff that we don’t want.
Now, I ideally we want a clear history (we don’t want the incomplete or wrong commit appear in the history), so we can use the following commands:
git reset --soft
With this command, all the stuff you changed in your latest commit will be uncommitted.
Then, you can use the ‘clean’ command to remove the bad files from your working directory:
git clean -f
Note :If you want to process directories as well as files use “-d” command along with “-f”.
Now, as if you haven’t commit yet, go ahead and make your fixes and commit again:
git commit -m "this awesome feature implementation, without the extra crap"
Using — soft vs — hard when doing reset
If you are don’t know the difference beween --soft and --hard use soft please. Why ?
If you choose --hard all the things that you have done in that commit will be GONE! as if you never did any of that. so be carefull when you use reset with --hard argument.
Using Graphical GIT Tools
Most of the graphical GIT tools have something similar that you can use to reset. For example with SourceTree, one of the popular GIT tools, you can right click on any commit in a branch history and click “Reset the current branch to this commit” option.
SourceTree reset branch option
If you use GIT regularly, I recommend to get this book and have it on your desk:
Cheers!
