Photo by Jiyeon Park on Unsplash

How to Revert to a Previous Commit in Git

Vanessa Martinez
2 min readFeb 1, 2021

--

I recently started learning Angular and building my first practice app. The first thing I learned to build was a component. Everything was going fine until I hit a bug that I wasn’t sure how to fix. I started to do research to understand the error, but then I realized I had two choices: 1) I could take some time to debug, which I was perfectly fine doing. 2) I could use the opportunity to explore Git commands.

Going Back in Time

I had already pushed my changes to Github, so I wanted to learn how to revert to a previous commit. So, the first thing I did was find the ID log (known as the “commit hash”) of the commit I wanted to revert back to, using the following command:

git log --oneline

That gave me a list of all previous commits and their IDs. I decided to go back into time with the commit that reads “added html to server comp.” Note: I wasn’t afraid to go back several commits, since this was a practice app, so it gave me the liberty to take risks. You might only need to revert back to a single previous commit.

Then, I took the commit hash and ran:

git revert --no-commit 62bbbd8..HEAD

After that, you should see the message below:

The git revert command reverts everything from the HEAD back to the commit hash. Essentially, it’s creating a new commit that’s equivalent to the previous one. That creates the illusion that we’re “undoing” everything, but, in fact, we do NOT lose any history. This is convenient if you’re working with others. The --no-commit flag lets you revert all commits at once, instead of having to handle them one by one.

After you’re done, you can continue to make changes. Don’t forget to git add and commit after you’ve added new changes.

Resources:

--

--

Vanessa Martinez

Freelance Software Engineer. Experience in JavaScript, React.js, and Ruby on Rails.