How do you roll back (reset) a Git repository to a particular commit?
This question already has an answer here:
How to revert Git repository to a previous commit?
4 Solutions collect form web for “How do you roll back (reset) a Git repository to a particular commit?”
git reset --hard <tag/branch/commit id>
git resetwithout the
--hardoption resets the commit history, but not the files. With the
--hardoption the files in working tree are also reset. (credited user)
If you wish to commit that state so that the remote repository also points to the rolled back commit do:
git push <reponame> -f(credited user)
Because of changes to how tracking branches are created and pushed I no longer recommend renaming branches. This is what I recommend now:
Make a copy of the branch at its current state:
git branch crazyexperiment
git branch <name> command will leave you with your current branch still checked out.)
Reset your current branch to your desired commit with
git reset --hard c2e7af2b51
c2e7af2b51 with the commit that you want to go back to.)
When you decide that your crazy experiment branch doesn’t contain anything useful, you can delete it with:
git branch -D crazyexperiment
It’s always nice when you’re starting out with history-modifying git commands (reset, rebase) to create backup branches before you run them. Eventually once you’re comfortable you won’t find it necessary. If you do modify your history in a way that you don’t want and haven’t created a backup branch, look into
git reflog. Git keeps commits around for quite a while even if there are no branches or tags pointing to them.
A slightly less scary way to do this than the
git reset --hard method is to create a new branch. Let’s assume that you’re on the
master branch and the commit you want to go back to is
Rename your current master branch:
git branch -m crazyexperiment
Check out your good commit:
git checkout c2e7af2b51
Make your new master branch here:
git checkout -b master
Now you still have your crazy experiment around if you want to look at it later, but your master branch is back at your last known good point, ready to be added to. If you really want to throw away your experiment, you can use:
git branch -D crazyexperiment
For those with a git gui bent, you can also use gitk.
Right click on the commit you want to return to and select “Reset master branch to here”. Then choose hard from the next menu.
When you say the ‘GUI Tool’, I assume you’re using Git For Windows.
IMPORTANT, I would highly recommend creating a new branch to do this on if you haven’t already. That way your master can remain the same while you test out your changes.
With the GUI you need to ‘roll back this commit’ like you have with the history on the right of your view. Then you will notice you have all the unwanted files as changes to commit on the left.
Now you need to right click on the grey title above all the uncommited files and select ‘disregard changes’. This will set your files back to how they were in this version.