Git reverts to the previous version

Git reverts to the previous version

1. Application scenarios

A wrong submission was made, and the code needs to be rolled back to a certain version; or a certain version of the code needs to be checked out, and then switched back to the latest version.

2. Solutions

2.1 Method 1: git reset

By command:git reset --hard 目标版本号

In this way, the entire system will fall back to 目标版本号the corresponding state, and the version submission records after this version will be gone.

This comparison applies to the situation where the target version is rolled back and then the version is directly discarded.

Before using this command, make sure that all local modifications have been committed, otherwise uncommitted files will be directly lost.

2.2 Method 2: git checkout

git checkoutIn addition to following the command 分支名称, you can also follow it. You can switch to the state of the target version 目标版本Idthrough the command . Note that this command is actually switching to the temporary branch of the target version. If you want to return to the latest version, you can use the command .git checkout 目标版本号git checkout 原来分支名称

This kind of comparison is suitable for temporarily going back to the previous version to check the current state, and then returning to the latest version.

2.3 Method 3: git revert

git revert is used to "reverse" a certain version to achieve the purpose of undoing the modification of this version. For example, we have committed three versions (version 1, version 2, and version 3), and suddenly found that version 2 is not working (for example: there is a bug), and we want to cancel version 2, but do not want to affect the submission of version 3, we can use The git revert command is used to reverse version 2 and generate a new version 4. In this version 4, the things of version 3 will be kept, but the things of version 2 will be revoked.
————————————————
Original link: https://blog.csdn.net/yxlshk/article/details/79944535

3. Reference link

[1]. Git restores the previous version by two methods reset and revert (detailed explanation with pictures and texts)

[2]. Git switches back to the old version code and then switches back to the latest code

[3]. Use git reset to roll back the version

[4]. Restore the problem of file loss caused by git reset --hard but not submitting all files to the warehouse

Guess you like

Origin blog.csdn.net/wml00000/article/details/124539962