How to Revert to a Previous Commit in Git without Modifying History

Published Aug 11, 2021

What is a safe way to rollback to a previous state in our Git repository without destroying or altering the history?

Suppose we want to revert back to a commit with the hash 521dd125.

git revert --no-commit 521dd125..HEAD
git commit -m "revert all in one"

This will revert everything from the HEAD back to the commit hash. We are restoring the state of the repository to that of the commit with hash 521dd125 by creating one single commit that is “undoing” all the changes in this commit tree.

The --no-commit flag allows us to revert all the commits in this tree at once. Without it, we’ll create commits for every commit we want to revert.

More CLI Articles