How to Revert Git Repository to a Previous Commit
- Remove Unpublished Commits to Revert Git Repository to a Previous Commit
- Remove Published Commits to Revert Git Repository to a Previous Commit
This article will discuss the various ways you can roll back a git repository to a previous commit. You can undo almost everything with Git.
We will look into both the local and remote repository and how you can undo unpublished and published commits.
Remove Unpublished Commits to Revert Git Repository to a Previous Commit
Unpublished commits are simply the commits in your local repository that are not yet pushed to the remote repository. We will use an example to explain this concept.
Example 1:
Below is the commit history in our local repository. Note the combination given to each commit message.
If for some reason, we find that the most recent commit has errors, we can delete the commit and revert the repository to its previous state.
In our case, the bad commit should be ba4c699
(Update Name). We run the git rest --hard <sha1-commit-hash>
command to delete this commit.
git reset --hard 00e1a53
Output:
HEAD is now at 00e1a53 Merge branch 'main' of https://github.com/Wachira11ke/Delftscopetech
The command above returns your repository to the specified commit and deletes the commits after it. The command will also delete all uncommitted changes in your repository.
If you want to keep them and apply them after deleting them, run the commands below.
git stash
git reset --hard 00e1a53
git stash pop
This combination saves the uncommitted changes and applies them to the new workspace. You may encounter merge errors if you have made modifications to files.
Remove Published Commits to Revert Git Repository to a Previous Commit
Published commits are the changes applied to the remote repository. If we pushed bad changes from our local repository to the remote repository, we could revert the repository to its previous version.
It is worth alerting other developers not to fetch from the repository. Let’s look at an example.
Example 2:
We have previously discussed how to remove commits in our local repository. Suppose we had already pushed the commit we deleted to the remote repository.
In the following context, we can revert the remote repository with the git push
command.
git push --force origin HEAD
The command above will overwrite our remote repository based on the state of our local repository. It will discard any changes made by other developers.
Here is a safer option:
git push --force-with-lease oriin HEAD
You can specify a branch using the command below:
git push -f origin <sha1-commit-hash>:branch_name
Some remote repositories have a receive.denyNonFastForwards
preset that rejects the command above. We will have to delete and recreate the branch in such a case.
git push origin : <branch name>
git push origin <sha1-commit-hash>:ref/heads/<branch name>
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn