How do you roll back (reset) a Git repository to a particular commit?

132    Asked by KiraMcelvain in Devops , Asked on Jun 16, 2025

How can you roll back a Git repository to a specific previous commit?

This question explores the different methods Git offers—like git reset, git checkout, or git revert—to undo changes and return your codebase to an earlier state safely.

Answered by Joshua Smith

Sometimes, things go wrong and you just want to hit "undo" in your Git project. Luckily, Git provides several ways to roll back or reset your repository to a previous commit — but each has different consequences, so it’s important to choose the right one for your situation.

 1. git reset – for local undo

If you haven’t pushed your changes yet, and want to completely remove commits:

  git reset --hard 

  • This moves your branch pointer to a previous commit.
  • --hard also changes the working directory and index.
  •  Be careful: this deletes any changes after that commit!

If you want to keep changes in your working directory:

  git reset --soft 

 2. git revert – for safe, undoable changes

If you've already pushed your changes and others are working on the repo:

  git revert 

  • This creates a new commit that undoes the changes.
  • It's safe for collaborative work because it doesn’t rewrite history.

 3. git checkout or git switch – for exploring

If you just want to see an older state:

  git checkout 

or:

  git switch --detach 

  • This puts you in a "detached HEAD" state to browse or test older commits without affecting anything.



Your Answer

Interviews

Parent Categories