Git Tutorial: reset, revert, gitrevert
One of the reasons for using the version control system is to save the changes made since the beginning of the development, so that you can easily retrieve the previous code or remove incorrect modifications. Even if a developer does not have a version control system, I develop it in one place and find that the direction is wrong. If I want to go back, I can only re-develop it, or modify the code to the previous state by memorizing it, it's a nightmare.
This article specifically introduces regret medicine: reset, revert.
Let's talk about the version first. We need to know which version to recover. Unlike svn, git only has a commit ID, which is a 40-bit hexadecimal string, to facilitate the operation, use HEAD to represent the last submission, and HEAD ^ to represent the last submission of HEAD:
Of course, it can also be used to indicate the nth submission from the last number, HEAD ~ N:
If you only roll back the last time, you can:
Git reset HEAD ^
Or:
Git revert HEAD
The two effects are equivalent, but the implementation is different:
Git reset HEAD ^
Git revert HEAD
The reset directly refers to the previous version of the HEAD. From the log, there is no last commit.
Revert revokes the last submitted modification and submits it to the server. From the log, there is an additional rollback commit, and the original commit is also in progress.
Also, by default, the reset does not modify the workspace file. After the rollback, the workspace file is still original and has not been rolled back.
If you add -- hard, the workspace will also be rolled back.
By default, the revert combines and submits the changes. If a conflict exists, manual modification and submission are required.
Note that only the last commit is equivalent. The reset function is to roll back, And the revert is to cancel.
Git reset HEAD ~ 3
Git revert HEAD ~ 3
We can see that if the parameter is a version, the revert only revokes the modifications made by this version, and then merges and submits the changes.
The reset is rolled back to this version, and all modifications made after this version are lost.
From the final result, the following command is equivalent:
Git reset HEAD ~ 3
Git revert HEAD ~ 3. HEAD
After revert <version a> .. <Version B>, from <Version B> to <version a>, but does not include <version a>, is actually a version rollback, each time, you are asked to input and submit comments.
Execute the preceding commands separately. If you read logs, you will find that three logs are missing after reset and three logs are missing after revert.
If you do not want to undo the revert, check the log and reset it.
What should I do after reset? No log is available! It doesn't matter. The following command can find more commit IDs:
Git reflog
Enter:
Git reset <commit ID>
This commit ID is the ID before the last reset, And you can roll back the previous status of the reset.