Mock interviewer: How does Git undo?

Posted May 28, 20203 min read

In Git, there are three commands that can be used to undo operations, namely git reset, git revert, and git checkout.

background knowledge

The Git warehouse is composed of three parts, the work area, the staging area, and the Commit history.

We develop in the workspace, and then add the file modifications to the staging area through git add, and then commit the changes with git commit, and commit all the contents of the staging area to the Commit history of the current branch.

git revert

This operation can only be applied to Commit, not files. At the same time, the operation will create a new Commit to undo the previously submitted changes, it will not modify the existing history.

In other words, this command is safe, it will not lose Commit records, it will only be added.

git reset

This command is relatively more complicated. It can act on Commit and files at the same time, and is used to undo changes that have not been submitted to the remote warehouse.

For Commit, git reset will move the position of HEAD, and you can also change the staging area and work area. There are three options for this operation:

  1. -soft:This option will modify the HEAD position, and the work area and temporary area will not change
  2. -mixed:The default option will modify the HEAD position and the temporary storage area, but the work area will not change
  3. -hard:modify the HEAD position, the temporary storage area and the work area will be updated to the specified Commit status

Isn't it a little bit, it doesn't matter if we talk in actual combat:

touch a
git add a
git commit 'feat:add a file'
touch b
git add b
git commit 'feat:add b file'
touch c
git add c
git commit 'feat:add c file'

Now, if we use git log, we can see that there are three commit records, and if we use git status, we can see that the status of the temporary storage area is empty.

Next, we use the git reset HEAD ~ 1 command, and then we use the git log and git status commands again. You can see that in the history of Commit, the position of HEAD has been modified to the position of commit b, while working We can also find the c file in the directory, and the c file was moved out of the temporary storage area, which is mixed.

Next, we submit the c file back to the original state again. This time we add --soft to the command. Let's look again. The position of HEAD is still modified, and the c file in the workspace still exists, different What is c is still in the temporary storage area.

Finally, we add --hard and take a look again. We can find that the HEAD has changed, and the c files in the temporary area and the work area have been removed. This parameter is more dangerous, and it is not recommended to be in the public branch. On the operation.

After finishing Commit, how to revoke the file? For files, the three parameters mentioned above have no effect. It will update the corresponding file in the temporary storage area to the version when Commit is specified.

Or the above example, if I use the command git reset HEAD ~ 1 c, what will happen? You can think about it, the answer is that it will update the c file to the version when the last Commit was, that is, the state that has not been created yet, so if you execute git status, you can find that there is a delete in the temporary area:c`, this is the revocation of the file.

git checkout

When you checkout Commit, you will point HEAD to the Commit you specified, and this no branch points to this HEAD, so after checkout, you will be in a detached HEAD branch. If you want to commit Commit to this branch, you should checkout one Branch out, and then commit.

When you checkout a file, the checkout command is similar to reset, but it does not update the staging area, only the work area. When you use git checkout <commit> <filename>, it will work The corresponding file in the area is updated to the state of the specified Commit version. When you use git checkout-<filename>, the modification of this file in the work area will be revoked.

For these operations, everyone must pay attention, do not use them indiscriminately, it is not good to mess up the warehouse.

Thank you very much for reading, welcome to follow, forward, share and support me.

Public number QR code.png