Recovering a reverted Git commit
This article outlines the steps required to revert a reverted Git commit. By the end of this article, you will have the necessary knowledge to recover a reverted commit without rewriting your commit history.
Recovering a reverted Git commit
Let's look at the following example.
In our repository we have the commit history as shown below.
We can revert our first commit using the git revert command as shown below.
$ git revert 9735f67
This command opens a text editor where we will provide the commit message for the revert.
How can we recover it without rewriting our commit history?
In this case, we have to create a new commit for the revert. For this case, you can use two commands.
The first method involves git cherry-pick
the revert command. We will use the revert command with the commit hash of the commit that was made before the revert commit (ie HEAD@{1}
). We will run the command below.
$ git cherry-pick 9735f67
Output:
$ git cherry-pick 9735f67
[master 8fdc845] Rollback
Date: Wed Aug 24 09:00:15 2022 +0300
2 files changed, 11 insertions(+), 6 deletions(-)
This command will create a copy of the specified commit and apply it to your working tree. You do not need to add or commit anything, as the command adds a new commit to the repository.
The second method involves reverting a revert. As shown below, we will use the git revert command to revert our first revert.
$ git revert bb1c611
This commit will open a text editor where we will provide a new commit message for the revert. Once completed, we can push to the remote repository without rewriting our commit history.
In summary, Git allows us to recover reverted Git commits. The git cherry-pick and git revert commands come in handy when we want to un-revert reverted commits without rewriting the commit history.
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.
Related Articles
Ignore everything except certain files in Git
Publish Date:2025/04/20 Views:151 Category:OPERATING SYSTEM
-
This article outlines the steps to make Git ignore all but a few files in a Git repository. The .gitignore file is a useful Git utility that allows us to tell Git which files to track and which files not to track. If you want your .gitignor
Git merge repository
Publish Date:2025/04/20 Views:160 Category:OPERATING SYSTEM
-
When working on a project with multiple team members having separate repositories for each team, at some point in time we come across a situation where we want to combine these separate repositories into one master repository to deploy all
Git push to another branch with a different name
Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM
-
git push It has a rich set of options that allow you to use the full power of Git. One of them is its source:destination refspecs parameters. We use these git push to go to a specific branch with a name of our choosing. Finally, we'll see s
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
恢复还原的 Git 提交
Publish Date:2023/03/31 Views:181 Category:Git
-
本文概述了还原还原的 Git 提交所需的步骤。 在本文的最后,您将掌握必要的知识,可以在不重写提交历史记录的情况下恢复还原的提交。