This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.
In daily work, we often work in the same branch with multiple colleagues, and sometimes we have some error submissions. There are two ways to undo these errors: reset and revert.
The fallback (reset)
Git reset resets the current location of the HEAD to the specified version. For example, we committed three times for a branch:
HEAD is now pointing to commit 3. What if we want to reset the code back to commit 1?
% git reset --hard 'commit 1 'Copy the code
Commit records for commit 2 and Commit 3 are gone and fall back to Commit 1.
Let’s practice:
Git log shows that I have committed three times in total, and the third time is the node to which the current HEAD points
Now I’m going to go back to the node that I committed the first time
Run the “git reset –hard target version” command to roll back the version
% git reset --hard e15faaf4
HEAD is now at e15faaf test1
Copy the code
As you can see, we go straight back to the first commit, and the second and third commit records disappear.
Let’s go back to the log
Do the git revert
What is doing the opposite?
Git revert is used to “reverse” a version in order to undo changes made to that version. For example, we commit three releases (version 1, version 2, and version 3) and suddenly discover that version 2 doesn’t work (e.g. If you want to undo version 2 without affecting a commit to undo version 3, you can use git revert to reverse version 2 and create a new version 4 that retains version 3 but undoes version 2. As shown below:
The do before:
After the anti do:
Actual operation:
Following our example above, we have committed three times so far, and we want to reverse commit2, i.e. keep commit1 and COMMIT3, and remove commit2
Do the reverse with “git revert -n” and commit with “git commit -m” :
We need to resolve the conflict ourselves and commit
% git revert -n 30df3872497b1c56814adf53c1022dc1342e1c93
% git commit -m 'test4'
Copy the code
How do I retrieve commit records that have disappeared due to rollback or reverse action
Some commit records will disappear, but what if you regret it and want to get it back
git reflog
You can view all action records for all branches (including deleted commit records and reset actions)
java % git reflog
faa135e (HEAD -> master) HEAD@{0}: commit: test3
783ef0a HEAD@{1}: commit: test2
2288ba8 HEAD@{2}: commit: test1
abddace HEAD@{3}: commit: test3
30df387 HEAD@{4}: commit: test3
Copy the code
From here we can find all the previous commit records, and then find the commit ID you need to restore to restore.
Git reset command git reset command
% git reset --soft 'commit id'
Copy the code
Git reset — soft: resets to a version. Only the commit information is retracted, but the content does not disappear. The contents of files in the workspace are still there. If you want to commit, simply commit.
The friendliness of this operation is that if we just want to roll back the COMMIT operation, but don’t want to restore the content, leaving everything in place, this operation is fine, ensuring that the code is not lost.
conclusion
Git reset –hard remitting a commit Git revert remitting a commit. You can use git reset –hard to revert a commit. To avoid code loss due to misoperation.