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.
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.
To get a straightforward understanding of how it works, I create a blank repository on github here, and create three commits as shown above.
The purpose of
git reset is to point HEAD to the specified version.
git logto view the commit log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
commit b0ef8f9125226af8f06ff1aba7c1f1fc83adea9b (HEAD -> master, origin/master) Author: debuginn <email@example.com> Date: Tue Sep 21 16:36:39 2021 +0800 feat add 3.go commit 338bf3e30983d34074f37a18b3ff80ea9bca75f0 Author: debuginn <firstname.lastname@example.org> Date: Tue Sep 21 16:36:09 2021 +0800 feat add 2.go commit 6b166ed34962da08d944e2b1d3f36d9015dd8f35 Author: debuginn <email@example.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.
git reset --hardcommand
➜ demo git:(master) git reset --hard 6b166ed34962da08d944e2b1d3f36d9015dd8f35 HEAD 现在位于 6b166ed feat add 1.go
1 2 3 4 5
commit 6b166ed34962da08d944e2b1d3f36d9015dd8f35 (HEAD -> master) Author: debuginn <firstname.lastname@example.org> Date: Tue Sep 21 16:35:16 2021 +0800 feat add 1.go
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.
git push -fto force a push to a remote
1 2 3
总共 0（差异 0），复用 0（差异 0），包复用 0 To github.com:debuginn/demo.git + b98f95e...6b166ed master -> master (forced update)
At 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 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.
git logto view the commit log
1 2 3 4
Author: debuginn <email@example.com> Date: Tue Sep 21 16:36:39 2021 +0800 feat add 3.go
Redo the operation with the
1 2 3 4 5
➜ demo git:(master) git revert 338bf3e30983d34074f37a18b3ff80ea9bca75f0 删除 2.go [master ef822b7] Revert "feat add 2.go" 1 file changed, 9 deletions(-) delete mode 100644 2.go
Check 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 <firstname.lastname@example.org> Date: Tue Sep 21 17:12:00 2021 +0800 Revert "feat add 2.go" This reverts commit 338bf3e30983d34074f37a18b3ff80ea9bca75f0. commit b0ef8f9125226af8f06ff1aba7c1f1fc83adea9b Author: debuginn <email@example.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.
git reset and
git revert are ways to revive workspaces and remote commits, but the two operations have very different results.
git resetwipes out all previous commits, pointing HEAD to the commit record it reset, and the corresponding commit record no longer exists.
git revertredoes 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.