Skip to content

什么是 仓库?

理解 Git 仓库 — .git folder, 工作 directory, and staging 区域.

你将学到

  • 什么仓库 (仓库) 是 and 什么它 包含
  • three 区域: 工作 directory, staging 区域, and history
  • 什么隐藏 .git folder 做
  • lifecycle 的 file: untracked, staged, committed

概念讲解

什么是仓库?

A repository (often shortened to repo) is a directory that Git is tracking. It contains your project files plus all the history of changes you have made.

You can think of a repository as a folder where Git has set up shop — it watches every file inside, lets you save snapshots (commits), and remembers the entire history of your project.

三个区域

Git divides your project into three logical areas:

  1. Working directory — the actual files you see and edit in your editor. This is where you write code.
  1. Staging area (index) — a temporary holding area where you place files you want to include in your next commit. You add files here with git 添加.
  1. Repository (.git directory) — the permanent history. When you git 提交, the staged snapshot is saved here. This is where all commits, branches, and tags live.

The flow is:

Working Directory  →  git add  →  Staging Area  →  git commit  →  Repository

The .git Folder

When you run git init, Git creates a hidden .git folder inside your project directory. This folder contains everything Git knows about your project:

  • 对象/ — all the compressed snapshots and file contents
  • refs/ — pointers to commits (branches and tags)
  • HEAD — a file pointing to the current branch
  • 配置 — repository-specific settings

Never delete or manually edit the .git folder — doing so can corrupt your repository and lose all history. If you want to stop tracking a project, you can safely delete the .git folder, but you will lose all history.

File Lifecycle

A file in a Git repository goes through states:

  • Untracked — Git sees the file but is not tracking it yet (git 添加 to track)
  • Staged — the file is in the staging area, ready to commit
  • Committed — the file is safely stored in the repository history
  • Modified — a tracked file has been changed since the last commit
untracked → (git add) → staged → (git commit) → committed
committed → (edit file) → modified → (git add) → staged → (git commit) → committed

Understanding these states is the key to knowing what git 状态 is telling you.

示例代码

              # Create a new directory and turn it into a Git repository
mkdir my-project
cd my-project
git init

# Git creates a hidden .git folder — let's look inside
ls -la .git

# Create a file — it is untracked
echo "Hello Git" > README.md

# Check the status — Git sees the file but is not tracking it
git status

# Stage the file — now it is in the staging area
git add README.md

# Check the status again — the file is now staged
git status

# Commit the file — now it is in the repository history
git commit -m "Add README"

# Check the status — working tree is clean
git status
            

这个 walks 通过完整 lifecycle: 创建仓库, 添加 file, staging 它, and committing. 运行 git 状态之后每个步骤到 see 如何 file moves 之间状态.

动手试一试

  • .gitignore Generator

    创建 .gitignore 到 keep unwanted files out 的你的仓库.

  • Code Diff

    Compare two versions 的 file — 相似到 什么 Git shows on 提交.

常见错误

错误写法

删除 .git folder and losing 所有项目 history

正确做法

.git folder 包含你的整个仓库 history. 从不删除它 除非你 intentionally 想要到 停止 tracking 项目. 制作 regular backups or 推送到 remote.

错误写法

Confusing staging 区域使用仓库

正确做法

Files in staging 区域 (之后 git 添加) 是 NOT yet 保存 in history. 它们是 仅保存当 你运行 git 提交. 如果你 编辑 staged file, 你需要到 git 添加它 再次.

相关速查表

练习 什么是 仓库?

1 个练习

练习 什么是 仓库?

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误