How can I rollback a git repository to a specific commit?

Learn how can i rollback a git repository to a specific commit? with practical examples, diagrams, and best practices. Covers git, github development techniques with visual explanations.

Mastering Git: How to Rollback Your Repository to a Specific Commit

Hero image for How can I rollback a git repository to a specific commit?

Learn the essential Git commands and strategies to safely revert your repository to a previous state, whether you need to undo recent changes or fix a critical bug.

In the world of version control, Git is an indispensable tool for developers. One of its most powerful features is the ability to navigate through your project's history and, when necessary, revert to a previous state. This article will guide you through the process of rolling back a Git repository to a specific commit, covering different scenarios and the commands you'll need.

Understanding Git Rollback Operations

Before diving into the commands, it's crucial to understand the two primary ways to 'rollback' in Git: git revert and git reset. While both achieve a form of undoing changes, they operate very differently and are suited for distinct situations. Choosing the right command is key to maintaining a clean and accurate project history.

flowchart TD
    A[Start] --> B{Need to undo changes?}
    B -->|Yes| C{Are changes already pushed to remote?}
    C -->|Yes| D[Use `git revert`]
    C -->|No| E[Use `git reset`]
    D --> F[Creates new commit to undo]
    E --> G[Rewrites history locally]
    F --> H[End]
    G --> H[End]

Decision flow for choosing between git revert and git reset

Option 1: git revert - Undoing Changes Safely

git revert is the safest way to undo changes, especially when those changes have already been pushed to a shared remote repository. Instead of deleting commits, git revert creates a new commit that undoes the changes introduced by a previous commit. This preserves the project history, making it ideal for collaborative environments.

git log --oneline
# Pick the commit hash you want to revert
git revert <commit-hash>

Basic git revert command

Option 2: git reset - Rewriting Local History

git reset is a more powerful command that rewrites your project's history. It moves the HEAD pointer and optionally the index and working directory to a specified commit. This command is best used for local changes that haven't been pushed to a remote repository yet, as rewriting shared history can cause significant problems for collaborators.

There are three main modes for git reset:

1. git reset --soft <commit-hash>

Moves HEAD to the specified commit, but keeps all changes from the undone commits in your staging area (index). Your working directory remains unchanged.

2. git reset --mixed <commit-hash> (Default)

Moves HEAD to the specified commit and resets the staging area to match it. Changes from the undone commits are moved to your working directory as un-staged changes. This is the default behavior if no flag is provided.

3. git reset --hard <commit-hash>

Moves HEAD to the specified commit, resets the staging area, AND discards all changes in your working directory that occurred after the target commit. This is a destructive operation and should be used with extreme caution, as it permanently deletes uncommitted changes.

git log --oneline
# Identify the commit you want to reset to
git reset --hard <commit-hash>

Example of git reset --hard

Pushing Changes After a Rollback

After performing a rollback, especially with git reset, you might need to update your remote repository. If you used git revert, you can simply git push the new revert commit. However, if you used git reset and effectively rewrote history, a regular git push will likely be rejected because your local history diverges from the remote. In this case, you'll need to force push.

# After git revert
git push origin <branch-name>

# After git reset (use with extreme caution on shared branches)
git push --force-with-lease origin <branch-name>
# Or, the more aggressive (but less safe) option:
git push --force origin <branch-name>

Pushing changes after a rollback