Git Handbook

01 · Nền tảng Git

Hiểu Working Tree, Index, HEAD, repository, cấu hình và những thuật ngữ cốt lõi trước khi học nhiều command.

1. Mental model: 4 vùng dữ liệu

Git dễ hiểu nhất khi bạn phân biệt rõ bốn vùng.

VùngÝ nghĩaLệnh thường tác động
Working TreeFile bạn đang nhìn và sửa.restore, editor
Index / StagingSnapshot dự kiến cho commit kế tiếp.add, restore --staged
Local repositoryCommit graph và objects trong .git.commit, reset, rebase
RemoteRepository tham chiếu qua network hoặc path.fetch, push, pull
Working Tree --git add--> Index --git commit--> HEAD/history
                                                                     --git push--> Remote

2. Cài đặt và cấu hình

git-config

Đọc và ghi cấu hình Git.
Official docs ↗
Cú pháp
git config [--global|--local|--system] <key> <value>
Hiểu đơn giản
--global áp dụng cho user hiện tại; --local chỉ áp dụng cho repository hiện tại.
git config --global user.name "Thinh"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --list --show-origin
Dùng --show-origin khi một cấu hình “không hiểu từ đâu xuất hiện”.

3. Tạo và lấy repository

git-init

Khởi tạo repository Git trong thư mục hiện tại.
Official docs ↗
Cú pháp
git init [-b <branch>]
Hiểu đơn giản
Tạo thư mục .git/; source code không tự được commit.
mkdir git-demo && cd git-demo
git init -b main
git status

git-clone

Tạo bản sao làm việc từ repository có sẵn.
Official docs ↗
Cú pháp
git clone <url-or-path> [directory]
Hiểu đơn giản
Clone lấy object database, refs cần thiết và tạo working tree.
git clone https://github.com/example/project.git
git clone ../central.git developer-a

4. Thuật ngữ phải nắm

Thuật ngữGiải thích
HEADReference biểu diễn vị trí checkout hiện tại; thường trỏ tới branch hiện tại.
CommitObject mô tả snapshot, parent commit(s), author/committer và message.
BranchMột movable reference trỏ tới commit.
TagReference thường dùng đánh dấu phiên bản/release; không tự di chuyển như branch.
originTên remote mặc định do clone thường tạo; không phải keyword bắt buộc.
upstreamBranch remote mà local branch đang tracking.
SHA / object IDIdentifier content-addressed của object Git.