Basic concepts
Let’s first understand the concepts of Git workspace, staging area and repository
# #Workspace: is the directory you can see on your computer.
Temporary storage area: In English it is called stage, or index. It is generally stored in the index file (.git/index) under the "git directory", so we sometimes call the temporary storage area the index (index).
Repository: The workspace has a hidden directory .git. This is not the workspace, but the Git repository.
The following figure shows the relationship between the work area, the staging area in the repository and the repository:
The left side of the figure is the work area, with the version library on the right. The area marked "index" in the repository is the staging area (stage, index), and the area marked "master" is the directory tree represented by the master branch.
We can see from the picture that "HEAD" is actually a "cursor" pointing to the master branch. Therefore, where HEAD appears in the command shown in the figure, it can be replaced with master.
The area identified by objects in the figure is the Git object library, which is actually located in the ".git/objects" directory, which contains various created objects and content.
When the "git add" command is executed on a file modified (or added) in the workspace, the directory tree of the temporary storage area is updated, and the contents of the file modified (or added) in the workspace are written. to a new object in the object library, and the object's ID is recorded in the file index of the temporary storage area.
When performing a commit operation (git commit), the directory tree in the temporary storage area is written to the version library (object library), and the master branch will be updated accordingly. That is, the directory tree pointed by master is the directory tree of the temporary storage area at the time of submission.
When the "git reset HEAD" command is executed, the directory tree in the staging area will be rewritten and replaced by the directory tree pointed to by the master branch, but the workspace will not be affected.
When the "git rm --cached <file>" command is executed, the file will be deleted directly from the temporary storage area, and the workspace will not be changed.
When the "git checkout ." or "git checkout -- <file>" command is executed, the files in the workspace will be replaced with all or specified files in the temporary storage area. This operation is dangerous and will clear changes in the workspace that have not been added to the staging area.
When the "git checkout HEAD ." or "git checkout HEAD <file>" command is executed, all or part of the files in the master branch pointed to by HEAD will be used to replace the staging area and the work area. document. This command is also extremely dangerous, because it will not only clear the uncommitted changes in the workspace, but also clear the uncommitted changes in the staging area.