Revert a Git repository to a previous commit
This article will discuss various ways to roll back a git repository to a previous commit. You can undo almost anything with Git.
We will look at local and remote repositories, and how to undo unpublished and published commits.
Delete unpublished commits to revert your Git repository to a previous commit
Unpublished commits are simply commits in your local repository that have not yet been pushed to the remote repository. We will explain this concept with an example.
Example 1:
Below is the commit history in our local repository. Note the combination provided for each commit message.
If for some reason we discover that a recent commit had an error, we can remove the commit and revert the repository to its previous state.
In our case, the bad commit should be ba4c699 (update name). We run git rest --hard <sha1-commit-hash>
the command to remove this commit.
$ git reset --hard 00e1a53
Output:
HEAD is now at 00e1a53 Merge branch 'main' of https://github.com/Wachira11ke/Delftscopetech
The command above will return your repository to the specified commit and remove the commits after it. The command will also remove all uncommitted changes in your repository.
If you want to keep them and apply them after removing them, run the following command.
$ git stash
$ git reset --hard 00e1a53
$ git stash pop
This combination saves your uncommitted changes and applies them to the new workspace. If you have made modifications to the file, you may encounter merge errors.
Delete published commits to revert your Git repository to a previous commit
Published commits are the changes applied to the remote repository. If we push wrong changes from our local repository to the remote repository, we can revert the repository to a previous version.
It's worth reminding other developers not to fetch from the repository. Let's look at an example.
Example 2:
We have previously discussed how to delete commits in a local repository. Assume that we have already pushed the deleted commits to the remote repository.
In the following context, we can git push
restore the remote repository using the 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.
This is a safer option:
$ git push --force-with-lease oriin HEAD
You can specify a branch with the following command:
$ git push -f origin <sha1-commit-hash>:branch_name
Some remote repositories have a receive.denyNonFastForwards
default that rejects the command above. In this case, we will have to delete and recreate the branch.
$ git push origin : <branch name>
$ git push origin <sha1-commit-hash>:ref/heads/<branch name>
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/opersys_10162.html
Related Articles
Git authentication
Publish Date:2025/03/28 Views:163 Category:Git
-
This article demonstrates connecting a local repository to a remote repository on GitHub/Gitlab without getting 身份验证失败 error messages. Creating a local repository from scratch in Git To create a local repository from scratch, fo
Log graphs in Git
Publish Date:2025/03/28 Views:96 Category:Git
-
This article shows you how to use git log the command to graphically view the commit history in Git. Viewing log graphs in Git The command git log displays all the repository history at once snapshots(commits) . This command has a default f
Git refresh remote branch
Publish Date:2025/03/28 Views:93 Category:Git
-
Git is considered to be the most accurate and the most used software by developers in their projects and can be operated by multiple developers simultaneously. It provides many unique and quirky features to the developers which are very dif
Updating Git on Mac
Publish Date:2025/03/28 Views:181 Category:Git
-
When working on Git, you should stay updated with the latest version to get its latest features. This article will discuss how to install and update the latest version of Homebrew and Git on your personal computer. Homebrew on Mac Homebrew
Enable Git Tab Auto-Complete
Publish Date:2025/03/28 Views:109 Category:Git
-
This tutorial demonstrates how to enable git tab autocompletion. Importance of enabling Git Tab auto-completion When developers work with source code, they mostly prefer Git as it is a very familiar and convenient platform for developers th
Restoring a repository in Git
Publish Date:2025/03/28 Views:158 Category:Git
-
Sometimes while using Git, we come across a situation where we want to pull the latest changes from the remote repository and it conflicts with the existing modifications or files, then we have to push those files to the storage. Git provid
Undo Git Stash Pop conflicts
Publish Date:2025/03/28 Views:178 Category:Git
-
You can undo this using the solutions in this article git stash pop with merge conflicts . We show you how to abort an erroneous stash pop operation and return to a clean state. But we also demonstrated a git stash pop way to resolve the co
Resolving Git stash conflicts without committing
Publish Date:2025/03/28 Views:72 Category:Git
-
This article outlines the steps you should follow to resolve Git stash conflicts without reverting or creating commits. For a simpler context, we will simulate a situation where the git stash pop command results in a conflict and try to res
Git Stash needs to be merged
Publish Date:2025/03/28 Views:128 Category:Git
-
Git is a stylish platform that provides us with many features, one of the main ones is storage. With this unique feature, we can accumulate a lot of unchanged work that we don't want to commit to our repository when the code is checked in;