Different ways to undo in Git
Several different ways that you can undo things in Git and why you should or shouldn't
Git has a load of ways to undo stuff and each of them has pros and cons. In this post I just want to highlight some use cases that can crop up during development and the git command that I recommend you run in order to overcome them.
When you edit a file and you want to undo it to the point of your last commit.
If you have made multiple changes to a file and you decide that they are all a bit rubbish or unnecessary (this happens more than I would like to admit) then you can undo everything to the point of your last commit by typing;
git checkout -- file_1.txt
When you edit a group of files and want to undo them all to the point of your last commit.
The process for if you want to repeat the above for multiple files is largely the same, I wanted to bring it out into its own section because I wanted to issue a word of warning first. Running the following code will revert all changed files back to their last committed state, which is fine so long as you are aware of what all those files are. I would advise running
git status to confirm that all the modified files you are about to change are what you would expect.
So long as you are happy the code you would need to run is;
git checkout -- .
You can also run a slightly safer command by chaining all the files together like this;
git checkout -- file_1.txt file_2.txt
Finally you can run the same command on only .txt files by typing;
git checkout -- *.txt
When you want a file to revert to an earlier commit and you want the history to be kept.
It might be that you have been working on something for a while and after a few commits you realise that things just aren’t going to work out, depending on the branching model you use you might just want to kill the branch, but if you want to keep the branch you are in and just go back to an earlier commit you can run the following commands.
First we want to know the hash for the commit we want to get back to;
This will show all the commits for the branch you are currently in, the string you are looking for will be a hash that looks something like
Once you have this you can run the following command;
git checkout 36d5e484cbb7df38d852b3875d09d8d9656c1aa4 file_1.txt
You will need to commit the fact you have reverted back.
If you wanted to go back to the version right before
36d5e484cbb7df38d852b3875d09d8d9656c1aa4 was made then you can run the following;
git checkout 36d5e484cbb7df38d852b3875d09d8d9656c1aa4~1 file
When you want your entire project to revert to an earlier commit and you want the history to be kept.
Just like when I shared the code for reverting a group of files back to your last commit you can pass some parameters that will apply a checkout to the entire project.
git checkout 39446e62247e84f318ca863e8665fa5b18df4c13 . will revert all files back to the commit at
Again you will need to commit your changes.
When you want a file to revert to an earlier commit but you do not want the history to be kept.
I do not know how to do this, answers on a postcard via our contact page please!
When you want an entire project to revert to an earlier commit and you do not want the history to be kept.
If you just don’t care about your history and want to quickly move the entire project back to a point in time, there is a command for that. Honestly this isn’t something I have had to do except in tutorials, but it seems to come up for others.
First we will want to find the hash for the commit we want to go back to, then we would run the following code;
git reset --hard 39446e62247e84f318ca863e8665fa5b18df4c13
After running the command if you run
git log you will see that any commits made after the commit in your hash will no longer be there.
You want to temporarily ignore changes made to your project.
If you want to temporarily ignore changes you have made since your last commit you can do so with the following command;
This will take your changes out of the loop momentarily.
When you want to incorporate them back in again you can run;
git stash pop