How to Revert a Git Repository to a Previous Commit
Read this tutorial and learn how to temporarily switch to different commit, hard delete unpublished commits, and undo published commits with new commits.
While working with Git, it is often required to get back to one of the older commits to view its state or to delete the commits coming after it. Below, we will discuss the mentioned scenarios.
How to temporarily switch to a different commit
To go back to an older commit temporarily, you can use the git checkout command by mentioning the commit hash:
git temporarily switch to a different commit, git checkout
git checkout <sha1-commit-hash>The command above will detach your <kbd class="highlighted">HEAD</kbd>, that is, leave you with no branch checked out. To make commits after detaching from your branch, you need to create a new branch on the basis of that commit by using the command below:
create new branch with git switch
git switch -c <new-branch-name>Instead of using the 2 commands above, you can run the command below to create a new branch on the basis of an older commit :
git create a new branch
git checkout -b <new-branch-name> <sha1-commit-hash>How to remove unpublished commits
If you want to delete the recent commits existing only on your local repository, run the command below:
git reset commits
git reset --hard <sha1-commit-hash>The command above will delete all the recent commits up to the one you have mentioned the hash for. The mentioned commit will be the most recent one
In case you have uncommitted local changes on your branch, this command will delete all of them.
If there are changes that you want to keep, act as follows:
git stash and git reset
git stash
git reset --hard <sha1-commit-hash>
git stash popThis saves the modifications, then re-applies that patch after resetting.
Merge conflicts may arise if you have modified things which were changed since the commit you reset to. If you mess it up, you can go back anytime. To learn how to do that, check out How to Undo Git Reset.
How to undo published commits
In this section, we will demonstrate two ways of undoing the published commits, depending on whether you want to save the history or not.
Remove the published commits
As it was demonstrated in the previous section, it is possible to remove local commits with the <kbd class="highlighted">git reset --hard</kbd> command. After that, you can run git push with <kbd class="highlighted">--force</kbd> option and remove the commits from the remote repository.
git push changes with the --force option
git push --force origin HEAD--force overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch.
safe way for force pushing
git push --force-with-lease origin HEAD--force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's.
Revert the published commits
If you have published your work and don't want to reset the branch, you can revert the commits. Use <kbd class="highlighted">git revert</kbd> so as not to rewrite any history:
git revert the commits
git revert <sha1-commit-hash>If you don't want to add a separate commit for revert, after reverting you can run <kbd class="highlighted">git rebase</kbd> in the interactive mode to combine the latest commit with the commit of the revert. To learn how to do that check out How to Combine Multiple Git Commits into One.
git checkout
git rebase -i HEAD~2The git revert Command
<kbd class="highlighted">Git revert</kbd> belongs to the “undo” category operations, yet it’s not a classic undo command. The <kbd class="highlighted">git revert</kbd> command is applied to undo changes to the commit history of the repository. The <kbd class="highlighted">git revert</kbd> command allows you to pick a single commit, converse the changes and, finally, generate a completely new one. In the scope of this tutorial, you will learn how to revert to the previous state by using Git commands.
Generally, we can distinguish 2 significant advantages of <kbd class="highlighted">git revert</kbd>. They are as follows:
- The revert command can be effectively applied to a specific commit.
- It doesn’t change the history of the project. This means that it’s a completely secure operation for commits, which were already sent to the shared repository.
The git revert and git reset Commands
The most significant difference between <kbd class="highlighted">git revert</kbd> and git reset is that the <kbd class="highlighted">git revert</kbd> command targets a specific commit not removing all the coming commits. On the contrary, using the <kbd class="highlighted">git reset</kbd> command will remove the overall coming commits. Let’s say you are going to undo a commit with the usage of the <kbd class="highlighted">git reset</kbd> command, you need to remove the whole commits coming after the targeted one. Generally, <kbd class="highlighted">git revert</kbd> is the most secure alternative of <kbd class="highlighted">git reset</kbd>. Although the process may seem quite complicated, it becomes pretty easy after going through it several times.