Using Git — how to go back to a previous commit

Tolani Benson
May 30, 2020 · 3 min read

Git & GitHub are powerful tools which allow us to track all our changes to our projects and, when we inevitably do something that breaks them, go back to a previous working state. We are all aware of this time-reversing magic, but not necessarily how to do it!

Here’s a quick and simple guide on how to turn back time on your project and revert to a previous version.

Alarm clock
Photo by Icons8 Team on Unsplash

You have two options here:

1) In your terminal you can type:

$ git log --oneline

This is where it is important you gave yourself descriptive commit messages! It will show in your terminal a list of commits along with the commit message you set originally. If you have decent commit messages and you know what you’re looking for you should be able to work out which version you want to go back to from here. If you want a bit more information such as the time, date, and author of each commit you can omit the --oneline tag.

2) You can go through your commit history on your GitHub repo via the GitHub website. This allows you to check the state of all the files in the repo at each commit to make sure you are going back to the correct version.

Screen shot of github repo
Screen shot of github repo
Screen shot of GitHub commits screen
Screen shot of GitHub commits screen

This is useful if you didn’t give yourself useful commit messages, or you’re just not sure exactly which version you need to go back to. Committing little and often, so that your change history is clear should save you from having to take this route.

Whichever option you use, take a note of the ID of the commit you want to revert to.

Use git checkout & the ID (in the same way you would checkout a branch) to go back:

$ git checkout <commit-id> .

Don’t forget the final ‘ .’ — You aren’t required to add this, and it may look like it has worked but if you leave this off it will take you to a new “detached head state” where you can make changes and it will allow you to make commits, but nothing will be saved and any commits you make will be lost.

This will take you to the version you wanted to go back to in your local environment.

In the same way that you would with any normal commit, you have to add all files and push to remote to save this state.

$ git add .
$ git commit -m "Reverting to <commit-id>"
$ git push

Give yourself a slightly more descriptive commit message — maybe why you are reverting!!

And that’s it! You’ve turned back time on your project! Hurrah!

The Startup

Get smarter at building your thing. Join The Startup’s +789K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +789K followers.

Tolani Benson

Written by

Ex-financial analyst turned software engineer. Lover of learning, coding, cake and dogs. Not in that order.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +789K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store