Introduction to Reverting to Previous Commit in Git
Git, a popular version control system, provides a powerful feature called “revert” that allows you to undo changes made in a previous commit. Reverting to a previous commit is useful when you want to discard the changes introduced by a specific commit without deleting the commit itself. In this article, we will explore how to revert to the last commit in Git and effectively undo the changes.
Understanding Git Revert
Git revert is a safe way to undo changes in Git without losing the commit history. Unlike other Git commands, such as reset or delete, which modify the commit history, git revert creates a new commit that undoes the changes introduced by a specific commit. This approach preserves the commit history while effectively reverting the changes.
Reverting to the Last Commit
To revert to the last commit in Git, follow these steps:
- Open your terminal or command prompt and navigate to the repository directory.
- Verify the status of your repository by running the command:
git status
. This ensures that your repository is clean and there are no uncommitted changes. - Execute the revert command:
git revert HEAD
. This command reverts the last commit and creates a new commit that undoes the changes introduced by the last commit. - Git will open a text editor to add a commit message for the revert. Provide a meaningful message describing the revert and save the file.
- Once you save the commit message, Git will create a new commit that effectively reverts the changes made in the last commit.
Understanding Revert Commit SHA
In addition to reverting to the last commit, you can also revert to a specific commit using its commit SHA. The commit SHA is a unique identifier for each commit in Git. To revert to a specific commit, follow these steps:
- Open your terminal or command prompt and navigate to the repository directory.
- Find the commit SHA of the commit you want to revert to. You can use commands like
git log
or Git GUI tools to identify the commit SHA. - Execute the revert command:
git revert commit-SHA
. Replacecommit-SHA
with the actual commit SHA you want to revert to. - Git will create a new commit that undoes the changes introduced by the specified commit.
Reverting a Merge Commit
When dealing with merge commits, the revert operation works slightly differently. Reverting a merge commit undoes the entire merge operation, effectively removing all changes introduced by the merge commit. To revert a merge commit, follow these steps:
- Open your terminal or command prompt and navigate to the repository directory.
- Identify the merge commit you want to revert using the commit SHA or other Git tools.
- Execute the revert command:
git revert -m 1 merge-commit-SHA
. Replacemerge-commit-SHA
with the actual merge commit SHA. - Git will create a new commit that undoes the entire merge operation and effectively removes the changes introduced by the merge commit.
Handling Reverted Commits
Once you have reverted a commit, it’s important to understand how it affects your repository and the commit history:
- Reverted Commit: The original commit you reverted remains in the commit history. However, its changes are effectively undone by the revert commit.
- Revert Commit: Git creates a new commit that undoes the changes of the reverted commit. This commit serves as a record of the revert operation.
- Commit History: The commit history reflects both the original commit and the revert commit, providing a clear record of

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.
+ There are no comments
Add yours