Reverting git commits: 2 simple ways

Thiago Ferreira
3 min readAug 19, 2023

--

Reverting git commits is that thing we don’t have to do very often, so we forget how it’s done when you need it the most (a failed Friday deployment, perhaps?)

Let’s walk through that in this small post, which serves as my own cheatsheet when I need it, and hopefully can serve you too 😄

Small disclaimer: This approach will work better when you already have changes merged in your main branch. We will create another commit reverting the changes. If you need to revert something and change the history, you'll have to use the rebase command instead.

Prerequisites

I'll assume the reader already knows how to use the following git commands:

  • git log: shows the commit history. We'll need this to find the commit hash we want to revert.
  • git add/commit/push: After you revert changes, you'll commit/push as you normally do.

#1 — Using git revert

To revert a single commit or an interval of commits, here's what you'll do:

# Find your origin branch, the one you want to revert things from
git checkout main

# Use git revert, passing the -n option to generate a single commit
# USe git log to find the hashs used bellow
git revert -n f21306c..e330c04 # OLD_COMMIT..RECENT_COMMIT
# For a single commit, you don't need the interval, so `git revert -n f21306c`

# At this point use, git status/diff to confirm the changes you want are there
git status # Shows affected files
git diff # Shows code changes

# If everything looks right, create a new branch and push:
git checkout -b reverted-branch
git add .
git commit -m "Reverted some commits 🙊"
git push origin reverted-branch

Note that I'm using the option -n on git revert. Without this option, changes will be committed right away to the working branch.

I prefer to take things more slowly and be sure of what I'm doing, so this option works best for me.

Feel free to omit the -n option as needed.

# 2 — Usando git diff + git apply

This is a more manual approach that will get the same result. Usually, git revert will be enough, but sometimes you may have a messier scenario that requires you to review more carefully each change. This approach may be useful in this case:

# Find your origin branch, the one you want to revert things from
git checkout main

# Using git log, find the commit you want to revert
git checkout f21306c

# Now, compare the current commit with main.
# Confirm the changes you want are indeed present
# If they are not, you may be at the wrong history point
git diff main

# If everything matches, save the differences into a patch file.
git diff master --full-index > changes.patch

# Now go back to main, and apply the changes
git checkout main
git apply changes.patch
rm changes.patch # Delete the patch file. We don't want to commit this.

# Now, checkout a new branch and push your commits
git checkout -b reverted-branch
git add .
git commit -m "Reverted some commits 🙊"
git push origin reverted-branch

I hope this was helpful! If these tips saved a failed Friday deployment for you, drop a comment 🙌

--

--