Two methods of GIT restoring the previous version: reset and revert


1、 Problem description

In the process of implementing multi-human cooperative program development with GitHub, we sometimes have the wrong submission. At this time, we hope to cancel the submission operation and let the program return to the appearance before the submission. This paper summarizes two solutions: reset and reverse.

2、 Background knowledge

Git version management and head understanding

For each commit using git, GIT will automatically string them into a timeline, which is a branch. If there is no new branch, there is only one timeline, that is, there is only one branch. In git, this branch is called the main branch, that is, the master branch. There is a head pointer to the current branch (in the case of only one branch, it points to the master, and the master points to the latest commit). Each version has its own version information, such as unique version number, version name, etc. As shown in the figure below, suppose there is only one branch:

3、 Solutions

Method 1: git reset

Principle: git reset is used to modify the position of the head, that is, to change the position of the head to a previous version, as shown in the figure below. Suppose we want to go back to version 1:

Applicable scenario: if you want to restore to a previously submitted version, and we do not want the version submitted after that version, we can use this method.

Specific operation:

1. View version number:

You can use the command “git log” to view:

You can also view it on the GitHub website:

2. Use the “git reset — hard target version number” command to roll back the version

Then use “git log” to view the version information. At this time, the local head has pointed to the previous version:

3. Use “git push – F” to submit changes:

At this time, if “git push” is used, an error will be reported, because the version that our local library head points to is older than that of the remote library

So we have to use “git push – F” to push it up

From the GitHub graphical interface, the head of the remote library has also pointed to the target version:

Fallback success!

Method two: git revert

Principle: git reverse is used to “reverse” a certain version in order to undo the modification of the version. For example, we commit three versions (version 1, version 2, and version 3), and suddenly find that version 2 does not work (for example, there is a bug). If you want to undo version 2 but do not want to affect the submission of version 3, you can use git revert The command reverses version 2 and generates a new version 4. In this version, the contents of version 3 will be retained, but the contents of version 2 will be canceled. As shown in the figure below:

Applicable scenario: if we want to cancel a previous version, but want to keep the version after the target version and record the whole version change process, we can use this method.

Specific operation:

For example, there are three files in the library , text.txt 、text2.txt。

1. View version number:

You can view it from the command line (enter git log)

As shown in the figure, the last two versions are called “add” text.txt ”(i.e. a new document has been added text.txt )”Add text2. TXT”. We don’t need it at this time text.txt This file, which means you don’t want “add” text.txt ”The operation of that version can be reversed by “add” text.txt ”This version.

You can also view the version number through the graphical interface of GitHub website

2. Reverse with “git revert – N version number” and submit with “git commit – M version name”
(1) Reverse, use the “git convert – N version number” command. The following command reverses the version number 8b89621:

git revert -n 8b89621019c9adc6fc4d242cd41daeb13aeb9861

Note: conflicts may occur here, so you need to modify the conflicting files manually. And the GIT add file name is required.
(2) Submit, using “git commit – M version name”, such as:

git commit -m "revert add text.txt" 

At this time, you can use “git log” to view the local version information. It can be seen that a new version has been generated, and the “add” is added text.txt ”Version, but retains “add text2. TXT” version:

3. Use “git push” to push the remote library

git push

To view the remote library version information displayed on GitHub:

At this time, you can view the files in the warehouse, and there are two left: 、text2.txt

Reverse success!

This article on the GIT recovery of the previous version of the two methods reset, revert (detailed explanation of the text) article introduced here, more related to git recovery before the version of the content, please search the previous articles or continue to browse the relevant articles below, I hope you will support developeppaer more in the future!