GLM 4.5 Air

Complete Guide: Git Revert to Previous Commit

Learn how to revert Git repository to previous commit using reset, revert, or branch creation methods. Complete guide with safety tips.

Question

How do I revert a Git repository to a previous commit?

How do I revert from my current state to a snapshot made on a certain commit?

If I do git log, then I get the following output:

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b
Author: Me <me@me.com>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

How do I revert to the commit from November 3, i.e. commit 0d1d7fc?

GLM 4.5 Air

How to Revert a Git Repository to a Previous Commit

You can revert to a previous commit using git reset --hard 0d1d7fc, which will move your current branch pointer to commit 0d1d7fc and discard all changes made after that commit. If you want to keep your changes but unstage them, use git reset 0d1d7fc, and if you want to preserve changes as uncommitted but untracked files, use git reset --keep 0d1d7fc.

Contents

Understanding the Different Reset Methods

There are several ways to revert to a previous commit in Git, each with different implications for your repository state. The method you choose depends on whether you want to preserve your work, discard it, or maintain a clean history.

Reset vs Revert: git reset moves the branch pointer to a previous commit, effectively removing newer commits from history. git revert creates new commits that undo changes, preserving history but adding new reversal commits.

The key difference is that git reset rewrites history (which can be problematic if you’ve shared the commits), while git revert creates new commits that reverse the changes, maintaining history integrity.

Using git reset to Revert to a Previous State

The most direct way to revert to a previous commit is using git reset with different modes:

Hard Reset

bash
git reset --hard 0d1d7fc

This command will:

  • Move your current branch pointer to commit 0d1d7fc
  • Discard all changes made after this commit
  • Remove all staged and unstaged changes permanently

Soft Reset

bash
git reset --soft 0d1d7fc

This command will:

  • Move your current branch pointer to commit 0d1d7fc
  • Keep all changes as staged changes
  • Allow you to commit these changes again with a new commit message

Mixed Reset (Default)

bash
git reset 0d1d7fc

This command will:

  • Move your current branch pointer to commit 0d1d7fc
  • Unstage all changes made after this commit
  • Keep changes as unstaged (modified) files

Keep Reset

bash
git reset --keep 0d1d7fc

This command will:

  • Move your current branch pointer to commit 0d1d7fc
  • Keep changes as untracked files
  • Prevent accidental loss of work

Important: After any reset operation except --soft, you may need to clean up untracked files:

bash
git clean -fd

Creating a New Branch at a Previous Commit

If you don’t want to alter your current branch but want to work from a previous commit, create a new branch:

bash
git checkout -b branch-name 0d1d7fc

This creates a new branch named branch-name starting from commit 0d1d7fc. Your current branch remains unchanged.

You can also use:

bash
git branch branch-name 0d1d7fc
git checkout branch-name

This approach is useful when:

  • You want to experiment with code from a previous state
  • You need to maintain your current work while exploring an older version
  • You’re unsure if you want to permanently revert changes

Using git revert Instead of Reset

If you want to undo specific commits while maintaining a clean history (especially important if your commits are already pushed to a shared repository), use git revert:

bash
git revert 25eee4c
git revert a867b4a

This creates new commits that reverse the changes from the specified commits. The advantage is that it doesn’t rewrite history, making it safe for shared repositories.

You can also revert multiple commits at once:

bash
git revert 25eee4c..a867b4a

Safety Measures and Best Practices

Before performing any reset operation, consider these precautions:

  1. Backup your work:

    bash
    git stash push -m "backup before reset"
    
  2. Verify the commit:

    bash
    git show 0d1d7fc
    
  3. Create a backup branch:

    bash
    git branch backup-branch
    
  4. Check what will be affected:

    bash
    git diff 0d1d7fc..HEAD --name-only
    
  5. Avoid hard reset on shared branches unless you’re certain no one else is working based on those commits.


Conclusion

Reverting to a previous commit in Git can be accomplished through several methods depending on your specific needs:

  • Use git reset --hard when you want to completely discard all changes after a specific commit
  • Use git reset --soft when you want to keep changes as staged for a new commit
  • Use git checkout -b to create a new branch at the previous commit without altering your current branch
  • Use git revert when you need to undo changes while preserving history (especially for shared repositories)

Always remember to backup your work before performing destructive operations like git reset --hard. If you’re working with a team or have pushed your commits to a remote repository, prefer git revert over git reset to maintain a clean, linear history.