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
?
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
- Using git reset to Revert to a Previous State
- Creating a New Branch at a Previous Commit
- Using git revert Instead of Reset
- Safety Measures and Best Practices
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
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
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)
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
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:
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:
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:
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
:
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:
git revert 25eee4c..a867b4a
Safety Measures and Best Practices
Before performing any reset operation, consider these precautions:
-
Backup your work:
bashgit stash push -m "backup before reset"
-
Verify the commit:
bashgit show 0d1d7fc
-
Create a backup branch:
bashgit branch backup-branch
-
Check what will be affected:
bashgit diff 0d1d7fc..HEAD --name-only
-
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.