This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022.

Author: Tangyuan

Personal blog: Javalover.cc

preface

The reset operation is mainly used to reset the contents of the workspace and staging area, and is mostly used to modify submitted records or restore local records.

The related areas of the reset operation are: workspace, staging area, version library;

Their relationship is shown below:

directory

  1. Reset the level of the operation
  2. Git reset –soft
  3. Git reset – Default
  4. Git reset –hard

The body of the

1. Reset the operation level

The reset operation can be divided into three levels, which are workspace, staging area, and local repository

  • Git reset –soft: changes only the Head pointer, that is, resets only the records in the local repository
  • Git reset –mixed: Defaults to changing the contents of the staging area while resetting the local repository
  • Git reset –hard: resets the local repository, staging area, and workspace simultaneously

Let’s take a look at the three levels;

2. Reset the local warehouse

Git reset –soft

The reset operation moves the HEAD pointer to the specified commit and does not modify the contents of the other sections.

For example, we now commit three times and have three versions in the local repository, as follows:

The examples in this article assume that there is only one master branch, not multiple branches

In this case, the version of the three areas is V3:

Git reset –soft HEAD~ will point the HEAD to V2 and the reset will be complete.

At this point, only the records of the local repository are modified, the workspace and staging areas are not modified;

The contents of the corresponding three zones are as follows: the local repository becomes V2, and the workspace and staging areas remain V3:

If we were to make changes locally and then recommit to the local repository, these operations would be similar to how we changed the committed records; Both of them ended up modifying the submission record;

3. Reset the temporary storage area

For the command: git reset

This reset operation is the default operation, which is to continue resetting the staging area after resetting the local repository, and then stop;

As shown below, the original three regions are the latest record V3:

After git reset HEAD~ is executed, the local repository and staging area will change to V2 and the workspace will remain unchanged.

Git status indicates that the contents of the staging area have been cleared.

Here’s an example:

The git log for the existing repository looks like this: As you can see, there are two records

jalon@bogon git-basic % git log
commit df79ed26f9fb8de7c721bd7df3a91a0cb114581b (HEAD -> master)
Merge: d488c1c cf228d6
Author: Jalon
Date:   Sat Jan 22 15:59:48 2022 +0800

    fix

commit cf228d6ebb0af2209406b7e58c19de79f1b800ec
Author: Jalon
Date:   Sat Jan 22 15:59:11 2022 +0800

    update a.txt
Copy the code
  1. To perform firstgit reset HEAD~Is reset tocf228The record

After the reset, there will be a nice prompt saying that the change record is not temporary; In other words, the change record in the staging area is deleted

jalon@bogon git-basic % git reset HEAD~
Unstaged changes after reset:
M	a.txt
Copy the code
  1. Git status:
jalon@bogon git-basic % git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code

This reset of the staging area is mainly to modify the commit record;

Git reset HEAD~ git reset HEAD~

4. Reset the local workspace

Git reset –hard

  • This command resets the contents of the local workspace, staging area, and local repository at the same time.

The –hard command is dangerous because local changes will be overwritten, and if the local changes have not been committed, they will not be recovered.

As shown below, the original three zones are V3:

Git reset –hard HEAD~

Git status is now available in the V2 version, so the workspace is clean as well.

  • The reset operation also serves to clear local operations:

For example, now that we’ve modified something and added it to the staging area (git add),

Git reset –hard (default: HEAD);

The local workspace and staging area will revert to the latest committed version pointed to by HEAD.

  • About the new documents:

If you have a new file (a non-modified file) in your local workspace, it simply exists in your local workspace and has not yet been added to git records.

In this case, the file will not be deleted (because the file does not exist in git system, it cannot be deleted through git command).

For example, if we create a new file c.txt, there are three files a.txt, b.txt, and c.txt

jalon@bogon git-basic % touch c.txt
jalon@bogon git-basic % ls
a.txt	b.txt	c.txt
Copy the code

Git reset –hard HEAD resets the contents of all sections to the latest commit pointed to by the HEAD:

jalon@bogon git-basic % git reset --hard HEAD
HEAD is now at 633f24c rebase demo
jalon@bogon git-basic % ls  
a.txt	b.txt	c.txt
Copy the code

Git status: $git, $git, $git, $git, $git, $git

jalon@bogon git-basic % git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	c.txt

nothing added to commit but untracked files present (use "git add" to track)
Copy the code

Git is not tracking the file, so git reset cannot interfere with it.

conclusion

Reset the three levels of the operation

  • git reset --soft: Only modifies the Head pointer, that is, only resetsLocal repositoryThe record of
  • git reset: Default: resetLocal repositoryAt the same time, it will be modifiedThe staging areaThe content of the
  • git reset --hard: Simultaneous resetLocal warehouse, temporary storage area, work area

Finally –hard is the more dangerous operation because it resets the local workspace modification record;

Before performing –hard, make sure that the contents of your local workspace have been committed to the repository (git commit so you can save it), unless you are really sure that the local workspace change record is useless