Let’s first understand the concepts of Git Workspace, Temporary Zone and Version Library
Workspace:It’s the catalogue you can see on your computer.
Temporary storage area:English is called stage, or index. Usually stored in the index file (. git / index) under the “git directory”, so we sometimes call the temporary storage area index.
Version Library:The workspace has a hidden directory. git, which is not a workspace, but a version Library of Git.
The following figure shows the relationship between workspace, temporary area in version library and version library:
The workspace is on the left and the version library is on the right. The area labeled “index” in the version library is the stage (index), and the area labeled “master” is the directory tree represented by the master branch.
As can be seen from the figure, “HEAD” is actually a “cursor” pointing to the master branch at this time. So where HEAD appears in the icon command can be replaced by master.
The area identified by objects in the figure is Git’s object library, which is actually located in the’.git/objects’directory. It contains all kinds of objects and contents created.
When the “git add” command is executed on a workspace modified (or added) file, the directory tree of the temporary area is updated, and the contents of the workspace modified (or added) file are written to a new object in the object library, and the ID of the object is recorded in the file index of the temporary area.
When git commit is performed, the directory tree of the temporary area is written to the version library (object library), and the master branch is updated accordingly. That is, the directory tree that the master points to 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 of the temporary area is rewritten and replaced by the directory tree pointed to by the master branch, but the workspace is not affected.
When the “git RM — cached < file >” command is executed, the file is deleted directly from the temporary area, and the workspace is unchanged.
When the “git checkout.” or “git checkout – < file >” commands are executed, the files in the workspace are replaced with all or specified files in the temporary area. This operation is dangerous and clears changes in the workspace that are not added to the temporary area.
When the command “git checkout HEAD.” or “git checkout HEAD < file >” is executed, all or part of the files in the master branch pointed by HEAD are used to replace the files in the temporary and workspace. This command is also very dangerous because it clears not only uncommitted changes in the workspace, but also uncommitted changes in the temporary area.