Master Git in 30 Days

Day 20: Fixing Committed Version History Part 2 (revert)

In the version control process, there is another common situation: after executing multiple commits, you discover that some previous commits were wrong. For example, you accidentally committed test code, causing problems in the current version. In this case, you need to understand the content explained in this article.

In Day 18, we learned about git reset and git commit --amend for fixing local commit history. However, these commands rewrite history and shouldn’t be used for commits that have been pushed to shared repositories. For shared history, git revert is the safe alternative.

Understanding git revert

git revert creates a new commit that undoes the changes from a previous commit. Unlike reset, it doesn’t rewrite history - it adds to it. This makes it safe for shared branches.

Suppose at this point you discover that a previous commit (such as HEAD~ or HEAD^ or a specific commit hash) was done incorrectly. You want to revert that specific version only, rather than resetting to the first version and redoing everything. In this case, you can try the git revert command, which can undo the changes of a certain version through “reverse” steps.

What does “reverse” mean? Let’s try this command immediately.

Important Note: Before executing the git revert command, make sure your working directory is clean! If you have files that are partially modified, it’s recommended to use git stash to create a temporary stash.

Basic Revert

To revert the most recent commit:

git revert HEAD

This creates a new commit that undoes the changes in the last commit.

Reverting a Specific Commit

Suppose from the git log history, you found a problematic version. You can first check the change record of this version. You can use git log to find the commit ID, then use git show [commit_id] to see the relevant information of that version.

To revert a specific commit:

git revert <commit-hash>

For example:

git revert a1b2c3d

During execution, you’ll be asked to edit the final commit message. By default, it adds the word Revert and also adds This reverts commit xxxx on the third line to tell you that this version’s main purpose is to revert from the xxxx version.

Reverting Multiple Commits

To revert a range of commits:

# Revert commits from HEAD~3 to HEAD~1 (3 commits)
git revert HEAD~3..HEAD

Or revert multiple specific commits:

git revert <hash1> <hash2> <hash3>

Revert Without Committing

To stage the revert changes without automatically committing:

git revert --no-commit HEAD
# or
git revert -n HEAD

This is useful when you want to revert multiple commits and create a single revert commit:

git revert --no-commit HEAD~3..HEAD
git commit -m "Revert last 3 commits"

Handling Conflicts

Sometimes reverting a commit can cause conflicts:

git revert <commit-hash>
# If conflicts occur...
# 1. Resolve conflicts in the files
# 2. Stage the resolved files
git add <resolved-files>
# 3. Continue the revert
git revert --continue

To abort a revert in progress:

git revert --abort

Reverting a Merge Commit

Reverting a merge is more complex because a merge commit has two parents. You must specify which parent to revert to:

# -m 1 means keep changes from the first parent (usually mainline)
git revert -m 1 <merge-commit-hash>

When to Use Revert vs Reset

Use git revert when:

  • The commit has been pushed to a shared repository
  • You want to preserve history
  • Working on a public branch (main, develop, etc.)
  • You need a record of the undo operation

Use git reset when:

  • The commit is only in your local repository
  • You want to completely remove commits from history
  • Working on a private feature branch
  • The commit was a mistake that shouldn’t be in history

Reverting and Re-applying Changes

Sometimes you revert a commit but later want to reapply it:

# First, revert a commit
git revert <commit-hash>

# Later, revert the revert
git revert <revert-commit-hash>

Practical Examples

Example 1: Revert a Bad Commit on Main Branch

# Commit a1b2c3d introduced a bug
git revert a1b2c3d
git push origin main

Example 2: Revert Multiple Commits

# Revert the last 5 commits as a single revert commit
git revert --no-commit HEAD~5..HEAD
git commit -m "Revert last 5 commits due to critical bug"
git push origin main

Example 3: Revert Then Make Corrections

# Revert a problematic commit
git revert a1b2c3d

# Make necessary corrections
# ... edit files ...

# Commit the corrections
git add .
git commit -m "Fix issues from reverted commit"
git push origin main

Best Practices

  1. Write clear messages: Explain why you’re reverting
    git revert a1b2c3d -m "Revert feature X due to performance issues"
    
  2. Test before pushing: Make sure the revert doesn’t break anything

  3. Communicate: Let your team know when you revert commits

  4. Consider alternatives: Sometimes a forward fix is better than a revert

  5. Document the reason: Reference issue numbers or provide context

Viewing Revert History

To see revert commits in your history:

git log --oneline --grep="Revert"

Today’s Summary

git revert is the safe way to undo changes in shared repositories. It maintains history integrity while undoing unwanted changes. Understanding when to use revert versus reset is crucial for effective collaboration and maintaining a clean, understandable project history.

Let me reorganize the Git commands and parameters learned today:

  • git revert
  • git revert HEAD
  • git revert
  • git revert –no-commit
  • git revert -n
  • git revert -m
  • git revert –continue
  • git revert –abort