Git Working Directory, Staging Area, and Repository
Basic concepts
Let's first understand the concepts of Git's working directory, staging area, and repository:
- **Working directory:** is the directory you can see on your computer.
- **Staging area:** called stage or index in English. It is generally stored in the index file (.git/index) under the .git directory, so we sometimes call the staging area the index.
- **Repository:** the working directory has a hidden directory .git, which is not part of the working directory but is Git's repository.
The following figure shows the relationship between the working directory, the staging area in the repository, and the repository:

-
In the figure, the left side is the working directory, and the right side is the repository. The area marked "index" in the repository is the staging area (stage/index), and the one marked "master" is the directory tree represented by the master branch.
-
From the figure we can see that "HEAD" is actually a "cursor" pointing to the master branch. So in the illustrated commands, wherever HEAD appears, it can be replaced with master.
-
The area marked objects in the figure is Git's object database, actually located under the ".git/objects" directory, which contains the various created objects and their contents.
-
When you execute the git add command on modified (or newly added) files in the working directory, the staging area's directory tree is updated, and the content of the modified (or added) files in the working directory is written to a new object in the object database, and that object's ID is recorded in the staging area's file index.
-
When a commit operation (git commit) is executed, the staging area's directory tree is written to the repository (object database), and the master branch is updated accordingly. That is, the directory tree that master points to is the staging area's directory tree at the time of the commit.
-
When executing git reset HEAD command, the staging area's directory tree is rewritten and replaced by the directory tree pointed to by the master branch, but the working directory is not affected.
-
When executing git rm --cached
command, files are deleted directly from the staging area, while the working directory remains unchanged. -
When executing git checkout . or git checkout --
command, all or specified files in the staging area are used to replace files in the working directory. This operation is dangerous, as it clears changes in the working directory that have not been added to the staging area. -
When executing git checkout HEAD . or git checkout HEAD
command, all or part of the files in the master branch pointed to by HEAD replace the files in both the staging area and the working directory. This command is also extremely dangerous because it not only clears uncommitted changes in the working directory, but also clears uncommitted changes in the staging area.
1. Working Directory
The working directory is the project directory on your local computer, where you create, modify, and delete files. It contains all files and subdirectories of the current project.
** Features:**
- Displays the current status of the project.
- File modifications are made in the working directory, but these changes have not yet been recorded in version control.
2. Staging Area
The staging area is a temporary storage area that contains snapshots of files to be committed to the repository. Before committing, you can selectively add changes from the working directory to the staging area.
** Features:**
- The staging area holds the changes that will be included in the next commit.
- You can use the
git addcommand multiple times to add files to the staging area until you're ready to commit all changes.
Common commands:
git add filename # 将单个文件添加到暂存区
git add . # 将工作区中的所有修改添加到暂存区
git status # 查看哪些文件在暂存区中
3. Repository
The repository contains all version history of the project.
Each commit creates a new snapshot in the repository; these snapshots are immutable, ensuring a complete history of the project.
Characteristics:
- Repositories are divided into local and remote repositories. Here we mainly refer to the local repository.
- The local repository is stored in the
.gitdirectory, which contains all committed objects and references.
Common commands:
git commit -m "Commit message" # 将暂存区的更改提交到本地版本库
git log # 查看提交历史
git diff # 查看工作区和暂存区之间的差异
git diff --cached # 查看暂存区和最后一次提交之间的差异
The relationship between the working directory, staging area, and repository
1. Working Directory -> Staging Area
Use the git add command to add changes in the working directory to the staging area.
git add filename
2. Staging Area -> Repository
Use the git commit command to commit changes in the staging area to the repository.
git commit -m "Commit message"
3. Repository -> Remote Repository
Use the git push command to push commits from the local repository to the remote repository.
git push origin branch-name
4. Remote Repository -> Local Repository
Use the git pull or git fetch command to fetch updates from the remote repository.
git pull origin branch-name
# 或者
git fetch origin branch-name
git merge origin/branch-name
Example
Suppose you modified file.txt in the working directory:
1. Working Directory
Modify file.txt and save it.
** 2. Staging Area**
Add the changes to the staging area:
git add file.txt
3. Repository
Commit the changes in the staging area to the local repository:
git commit -m "Update file.txt"
4. Remote Repository
Push the local commit to the remote repository:
git push origin main
By understanding the roles of the working directory, staging area, and repository and their interrelationships, you can use Git more efficiently for version control and collaborative development.