In team development, using Git as a versioning tool makes it easy to collaborate with multiple people to manage parallel development. However, if you or someone else’s commit contaminates a remote branch, you need to restore the remote code.
Git provides two commands to do this: reset and revert. These two commands have very different effects, so if you’re not sure how they work, you’ll need to understand them to avoid making a mistake that could lead to irreversible changes to your remote repository.
First of all, from the English definition, reset is the meaning of reset, revert is the meaning of recovery, restore, as a Coder, the first feeling reset effect is more violent than revert some, the actual situation is also true, let us discuss it together.
Background
Every commit in Git is a commit. You can see above that there are three commits in the timeline, and at this point HEAD points to the main branch, and the main branch points to the latest commit3.
- HEAD is a pointer to the latest commit in the current branch, and you can switch between any branches.
- The main (master) branch, which is the master and default branch of a git repository.
- commit generates a commit id for each commit to identify changes to the workspace.
Practice
To get a straightforward understanding of how it works, I create a blank repository on github here, and create three commits as shown above.
|
|
Git Reset
The purpose of git reset
is to point HEAD to the specified version.
-
Use
git log
to view the commit log1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
commit b0ef8f9125226af8f06ff1aba7c1f1fc83adea9b (HEAD -> master, origin/master) Author: debuginn <debuginn@icloud.com> Date: Tue Sep 21 16:36:39 2021 +0800 feat add 3.go commit 338bf3e30983d34074f37a18b3ff80ea9bca75f0 Author: debuginn <debuginn@icloud.com> Date: Tue Sep 21 16:36:09 2021 +0800 feat add 2.go commit 6b166ed34962da08d944e2b1d3f36d9015dd8f35 Author: debuginn <debuginn@icloud.com> Date: Tue Sep 21 16:35:16 2021 +0800 feat add 1.go
Here you can see that we committed the record three times, and we now want to revert to the first commit commit.
-
use the
git reset --hard
commandCheck the
git log
again:At this point we can see that we have reverted back to the first commit, which we are currently doing with
git reset --hard
.At this point, we’re just talking about the local HEAD pointing to commit 1 of the main branch, but the remote hasn’t changed, so we need to force a push at this point.
-
Use
git push -f
to force a push to a remoteAt this point we can see that the remote also does not have the three records we submitted before but only the first submission record.
When working together in a team on a repository, the git reset command must be used carefully, and when using it, you must double-check whether other people’s code will be reset and cause the code to be lost, resulting in the loss of some commit records, which are irreversible and must be used carefully.
Git revert
git revert
is used to redo the contents of a commit. In our original commit, we found that a new commit was created on top of the branch, and none of the commits we wanted to redo existed.
-
Use
git log
to view the commit log -
Redo the operation with the
git revert
commandCheck the git log again:
1 2 3 4 5 6 7 8 9 10 11 12 13
commit ef822b71c33a2dbbdaa350fddcfa14e8fc55e543 (HEAD -> master, origin/master) Author: debuginn <debuginn@icloud.com> Date: Tue Sep 21 17:12:00 2021 +0800 Revert "feat add 2.go" This reverts commit 338bf3e30983d34074f37a18b3ff80ea9bca75f0. commit b0ef8f9125226af8f06ff1aba7c1f1fc83adea9b Author: debuginn <debuginn@icloud.com> Date: Tue Sep 21 17:05:39 2021 +0800 feat add 3.go
You can see that the commit 2 commit has been redone and the 2.go has been removed.
You can see that there are four commits on github.
Summary
Both git reset
and git revert
are ways to revive workspaces and remote commits, but the two operations have very different results.
git reset
wipes out all previous commits, pointing HEAD to the commit record it reset, and the corresponding commit record no longer exists.git revert
redoes a selected commit, and if there is a commit after that, the commit record still exists, but the specified commit is cleared out.
Choosing the right way to roll back your code is important in teamwork, but be careful not to lose your code.