跳轉到

1.3 Git 的三大工作區域#

檔案旅行的三站#

理解 Git 的關鍵:你的檔案會在三個區域之間移動。

工作區 ──git add──▶ 暫存區 ──git commit──▶ 儲存庫
(Working Directory)  (Staging Area)      (Repository)
   你正在編輯          準備提交的內容        正式的版本快照
區域 位置 用途
工作區 你眼前看到的檔案 寫程式碼的地方
暫存區 .git/index 這次 commit 要包含哪些變更,先挑好
儲存庫 .git/objects 所有歷史版本的永久快照

為什麼要有暫存區?因為你可能同時改了三件事,但只想提交其中一件。git add 就是「挑選」的動作。

建立你的第一個 Repo:git init#

mkdir my-project
cd my-project
git init

執行後會看到:

Initialized empty Git repository in /path/to/my-project/.git/

.git 是一個隱藏資料夾,儲存庫的一切都在裡面。不要手動修改或刪除它——刪了它,這個專案就不再是 Git 專案了。

驗證一下:

git status
# On branch main
# No commits yet
# nothing to commit (create/copy files and use "git add" to track)

No commits yet 代表這是一個全新的空儲存庫,準備好接受你的第一次提交了。

已經有現成的專案?直接在專案根目錄執行 git init 即可,不必新建資料夾。