DEV Community

Cover image for How to Safely Roll Back to a Specific Git Commit in Your Local Project
K-kibet
K-kibet

Posted on

How to Safely Roll Back to a Specific Git Commit in Your Local Project

Mistakes happen — maybe you pushed the wrong changes, introduced a bug, or simply want to go back to a cleaner point in your project. The good news? Git makes it easy to roll back to a previous commit without panic. In this guide, I'll walk you through exactly how to return your local Git project to an earlier commit safely, depending on whether you want to keep or discard your recent changes.


🔍 Understanding Git Reset Options

Before rolling back, it’s important to understand the three main types of Git resets. Each determines what happens to your working directory and staged files.


1. Soft Reset — Undo a Commit but Keep All Your Changes

If you only want to move the commit pointer backwards but keep your files staged for re-commit:

git reset --soft <commit-hash>
Enter fullscreen mode Exit fullscreen mode

When to Use:

  • You mistakenly made a commit too early.
  • You want to combine commits or tidy your history.

Your files remain intact — only the commit is undone.


2. Mixed Reset — Keep Changes but Unstage Them

This is the default reset mode and the most commonly used:

git reset --mixed <commit-hash>
Enter fullscreen mode Exit fullscreen mode

When to Use:

  • You want to undo commits but still keep the changes in your files.
  • You prefer to re-stage the changes manually.

Your working directory stays the same; your staging area is cleared.


3. Hard Reset — Completely Discard All Changes

If you want to fully roll back and wipe out all changes made after a certain commit:

git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

When to Use:

  • You want a clean reset.
  • You’re sure you don’t need any recent changes.
  • Your local branch is a mess and you want to start fresh from a known good state.

⚠️ Warning: This permanently deletes all uncommitted changes. Use this only if you're absolutely sure.


🔎 How to Get the Commit Hash

Before using any reset command, find the commit you want to roll back to:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

You’ll see output like:

abc1234 Fix login bug
def5678 Add new payment system
...
Enter fullscreen mode Exit fullscreen mode

Copy the hash (e.g., abc1234) and use it with your reset command.


🚀 Rolling Back After You’ve Already Pushed

If the commit is already pushed to a remote branch and you're sure you want to rewrite history:

git push origin <branch-name> --force
Enter fullscreen mode Exit fullscreen mode

⚠️ Use with caution. Force-pushing can overwrite others’ work.


🎯 Final Thoughts

Rolling back to a specific commit doesn’t have to be scary. Git gives you powerful tools to safely undo mistakes — as long as you pick the right type of reset for your situation. Whether you want to keep your changes, clean your entire working directory, or just tidy up your history, these commands give you full control.

Top comments (0)