Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Working Directory, Staging Area, and Repository

Git Working Directory, Staging Area, and Repository


Basic concepts

Let's first understand the concepts of Git's working directory, staging area, and repository:

The following figure shows the relationship between the working directory, the staging area in the repository, and the repository:

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:**

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:**

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:

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.