Let’s first understand the concepts of Git workspace, staging area and repository:
- Workspace: This is the project directory which you can see on your computer.
- Temporary storage area: This is called stage or index. It is generally stored in the index file (
.git/index) in the .git directory, so we sometimes call the staging area an index.
- Repository: The workspace has a hidden directory .git , which is not a workspace, but a Git repository.
Relationship between workspace, staging area & repository
The following diagram shows the relationship between the workspace, the staging area in the repository, and the repository:
- The left side of the figure is the workspace, and the right side is the repository. 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.
- In the figure, we can see that “HEAD” is actually a “cursor” pointing to the master branch. Therefore, the place where HEAD appears in the command shown in the figure can be replaced by master.
- The area identified by objects in the figure is the object library of Git, which is actually located in the “.git/objects” directory, which contains various objects and contents created.
- When the git add command is executed on the modified (or newly added) files in the workspace, the directory tree in the staging area is updated, and the contents of the modified (or newly added) files in the workspace are written to a new one in the object library. object, and the ID of the object is recorded in the file index of the temporary storage area.
- When a commit operation (git commit) is performed, the directory tree in the staging area is written to the repository (object repository), and the master branch will be updated accordingly. That is, the directory tree pointed to 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 staging 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 the specified files in the staging area. This operation is dangerous and will clear changes from the workspace that were not 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 files in the staging area and the work area. This command is also extremely dangerous, as it will clear not only uncommitted changes in the workspace, but also uncommitted changes in the staging area.
In this article we saw the relationship between GIT workspace, Staging areas and Repositories. We saw the difference between these terms and their requirements.