The letter A styled as Alchemists logo. lchemists
Published October 31, 2019 Updated June 14, 2020

Git Revert

Demonstrates how to elegantly revert an existing Git commit.


# Hello and welcome to the Alchemists Screencasts!
# Today, we'll look at revering Git commits.
# We'll start by studying the commit history of this project:


# The second commit on the `master` branch is what we want to revert:
git revert <SHA>

# Notice the subject is a *wrapped* duplicate of the original subject.
# Sadly, the body doesn't explain *why* the commit was reverted either.
# We can do better by passing the `--no-commit` option to `git revert`.
# Let's start over by removing the reverted commit:

git reset --hard HEAD~1

# This time we'll explain the *what* and *why* of our actions.

git revert --no-commit <SHA>
git status --short --branch
git diff --cached

# Notice the `` file is cached.
# We can now make a *modified* commit of the original.
# Additionally, the original Table of Contents is restored but outdated.
# We'll regenerate the Table of Contents to pick up latest changes:

bundle exec rake toc
git diff

# Excellent, the Tocer gem detected the *Usage* section was removed from the Table of Contents.
# To proceed, we'll commit the changes.
# Also, pay attention to how Git pre-populates our commit message:

git add
git commit


# There, much better.

# Using Git revert in this fashion provides the following benefits:
# 1. Allows reuse of an old commit in a modified way -- Handy when resurrecting old ideas.
# 2. Keeps our Git history clean by crafting an understandable commit.
# 3. Explains *why* the original commit was reverted.

# Enjoy!
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄