Simulated Interviewer: how does git undo?

Time:2021-9-29

In git, there are three commands that can be used to undo an operation:git resetgit revertgit checkout

background knowledge

Git warehouse consists of three parts: workspace, staging area and commit history.

We develop in the workspace, and then throughgit addAdd file modifications to the staging area, and thengit commitBy submitting the changes, all the contents of the staging area are submitted to the commit history of the current branch.

git revert

This operation can only work on commit, not on files. At the same time, this operation will create a new commit to undo the previously committed changes, and it will not modify the existing history.

In other words, this command is safe. It will not lose the commit record, but only add.

git reset

This command is relatively complex. It can act on both commit and file to undo changes that have not been submitted to the remote warehouse.

For commit,git resetThe position of the head will be moved, and the staging area and workspace can also be changed. There are three options for this operation:

  1. –Soft: this option will modify the position of the head, and the workspace and staging area will not change
  2. –Mixed: the default option. The head location and staging area will be modified, but the workspace will not change
  3. –Hard: modify the head location, and the staging area and workspace will be updated to the specified commit status

It doesn’t matter if it’s a little around. Let’s 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, we usegit logYou can see threecommitRecord, usegit statusYou can see that the status of the staging area is empty.

Next, we usegit reset HEAD~1Command, and then we use it againgit logandgit statusCommand, you can see that in the commit history, the position of head has been changed to the position of submitting B. at the same time, we can find the C file in the working directory, and the C file has been moved out of the temporary storage area, which ismixed

Next, we submit the C file back to the original state again. This time, we add after the command--soft, let’s look again. The location of head is still modified, and the workspace C file still exists. The difference is that C is still in the staging area.

Finally, we add--hardLet’s take another look. We can find that the head has changed and the C files in the staging area and workspace have been removed. This parameter is dangerous, so we are not recommended to operate on the public branch.

After committing, how is the file revoked? For files, the three parameters mentioned above do not work. It will update the version of the corresponding file in the staging area to the specified commit.

Or the example above, if I use the commandgit 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 at the time of the last commit, that is, the state that has not been created, so you can execute itgit statusCan be found in staging areadelete: c, this is the revocation of the file.

git checkout

When you check out a commit, the head will point to the commit you specified, and this branch does not point to this head. Therefore, after checking out, it will be in a branch of detached head. If you want to submit a commit to this branch, you should check out a branch first, and then submit.

When you check out a file, the check out command is a little similar to reset, but it does not update the staging area, only the workspace. When you usegit checkout <commit> <filename>The corresponding files in the workspace will be updated to the state of the specified commit version when you usegit checkout -- <filename>The changes to this file in the workspace will be undone when.

For these operations, we must pay attention not to use them indiscriminately. It’s not good to mess up the warehouse.

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

Recommended Today

IOS Association object

1. Category and extension 1.1 category: category Specifically used to add new methods to classes; You cannot add a member attribute to a class, add a member variable, or get it,be careful:You can use runtime to add attributes to a classification; Used in [email protected] variables only generates variablessetter,getterCannot generate method implementations and underlined member variables. […]