How to go back multiple commits in Git with Git revert

In this quick tutorial we’re going to look at rewinding “time” in Git. Say you’ve already made a commit and decide to go back to that commit… Well it’s not too hard. Check out ALL our git tutorials here.

Generally speaking you want to KEEP everything in your history. You do not want to erase commits you don’t want. Instead, just let them be part of the history of the project. You’ll see what I mean in a moment. Here’s the project setup: Note. You should already have a basic understanding of git enough to follow the below instructions so you can follow along.

I have a git initialized folder with 3 files. I have 3 commits. index.txt was committed with the first commit. 2file.txt was committed with the 2nd commit. 3file.txt was committed with the third commit.

Run $ git log and your commit history should look similar to mine below:

And my text editor you can see has 3 files and 3 commits:

Now let’s say you want to get back to commit 2. Instead of DELETING commit 3, you’re instead going to make a new 4th commit that says you’re reverting back to commit 2. So you’ll now have a total of 4 commits, and commit 2 will exist in the history twice.

To do this you have to get the commit id from the commit you want to revert away from. In my case, I’m currently on the 6e7777 commit, and I want to get back to the 45e5d commit.

So all I would have to do is run this command:

$ git revert 6e7777d21979f4ee7522b507b82a6d3a6424cd02

Then it opens a text editor, which you’ll save the file as you want the commit message to read. In my case below “Revert ‘commit 3′”

Now run $ git log and you should see a 4th commit. So you’re BACK to commit 2, but you’ve still got commit 3 in your history.

Going back multiple commits? Almost as easy…

So in the documentation there’s reference to an even easier method, but I find it a little more confusing using their counting method. If you want the quickest way, just run a command similar to their examples in the documentation here: https://git-scm.com/docs/git-revert

I prefer to revert multiple commits by manually pasting in the commit ids in order from most recent commit to oldest commit. Look at the above image and watch how I get back to commit 1 below:

git revert --no-commit most_recent_commit 2nd_most_recent_commit 3rd_most_recent etc..

If you went back multiple commits with the “no commit” flag, then you’ll want to commit a message once you get back to where you want to be.

git commit -m "back to commit 1"

Now if you look in your project you should only see 1 file that was there when you made your first commit. If you look at your $ git log you still have the entire history of your project!

Thanks Stackoverflow. https://stackoverflow.com/questions/1463340/how-to-revert-multiple-git-commits

A note on git reset

git reset is basically the same result as git revert. The difference however… is that git revert doesn’t overwrite existing history and is much safer to use when working on teams and reverting changes that have been pushed to a remote repository. Git reset will erase your commits as if they never existed. You only want to use git reset when you haven’t pushed changes to a remote repo.

Leave a Comment

Your email address will not be published. Required fields are marked *

want more details?

Fill in your details and we'll be in touch