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ĩa | Lệnh thường tác động |
|---|---|---|
| Working Tree | File bạn đang nhìn và sửa. | restore, editor |
| Index / Staging | Snapshot dự kiến cho commit kế tiếp. | add, restore --staged |
| Local repository | Commit graph và objects trong .git. | commit, reset, rebase |
| Remote | Repository tham chiếu qua network hoặc path. | fetch, push, pull |
Working Tree --git add--> Index --git commit--> HEAD/history
--git push--> Remote2. Cài đặt và cấu hình
git-config
Đọc và ghi cấu hình Git.
git config --global user.name "Thinh"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --list --show-originDù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.
mkdir git-demo && cd git-demo
git init -b main
git statusgit-clone
Tạo bản sao làm việc từ repository có sẵn.
git clone https://github.com/example/project.git
git clone ../central.git developer-a4. Thuật ngữ phải nắm
| Thuật ngữ | Giải thích |
|---|---|
| HEAD | Reference biểu diễn vị trí checkout hiện tại; thường trỏ tới branch hiện tại. |
| Commit | Object mô tả snapshot, parent commit(s), author/committer và message. |
| Branch | Một movable reference trỏ tới commit. |
| Tag | Reference thường dùng đánh dấu phiên bản/release; không tự di chuyển như branch. |
| origin | Tên remote mặc định do clone thường tạo; không phải keyword bắt buộc. |
| upstream | Branch remote mà local branch đang tracking. |
| SHA / object ID | Identifier content-addressed của object Git. |