Git Handbook

08 · Công cụ Git nâng cao

Stash, tags, worktree, sparse checkout, submodules, clean và archive cho các workflow phức tạp hơn.

1. Stash

git-stash

Tạm cất thay đổi working tree/index để quay lại sau.
Official docs ↗
Cú pháp
git stash [push|list|show|apply|pop|drop]
Hiểu đơn giản
Phù hợp cho WIP ngắn hạn; đừng biến stash thành kho lưu trữ dài hạn.
git stash push -u -m "WIP payment"
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
git stash pop
git stash drop stash@{0}

2. Tag & release

git-tag

Tạo/list/delete tag.
Official docs ↗
Cú pháp
git tag [-a] <name>
Hiểu đơn giản
Annotated tag lưu metadata/message; thường phù hợp cho release.
git tag -a v1.0.0 -m "Release v1.0.0"
git show v1.0.0
git push origin v1.0.0
git push origin --tags

3. Worktree — nhiều branch cùng lúc

git-worktree

Quản lý nhiều working trees gắn với cùng repository.
Official docs ↗
Cú pháp
git worktree add <path> [branch]
Hiểu đơn giản
Giúp mở main, feature và hotfix song song mà không phải switch/stash liên tục.
git worktree add ../project-hotfix -b hotfix/payment main
git worktree add ../project-login feature/login
git worktree list
git worktree remove ../project-hotfix
Mỗi branch bình thường chỉ có thể checkout trong một worktree tại cùng thời điểm.

4. Repository lớn

git-sparse-checkout

Chỉ materialize một phần cây file trong working tree.
Official docs ↗
Cú pháp
git sparse-checkout init --cone
Hiểu đơn giản
Hữu ích với monorepo rất lớn khi bạn chỉ cần vài thư mục.
git sparse-checkout init --cone
git sparse-checkout set apps/web services/auth

git-submodule

Quản lý repository được tham chiếu bên trong repository khác.
Official docs ↗
Cú pháp
git submodule <command>
Hiểu đơn giản
Superproject lưu commit cụ thể của submodule; workflow cập nhật cần chủ động.
git submodule add https://github.com/org/shared.git libs/shared
git clone --recurse-submodules URL
git submodule update --init --recursive

5. Clean & Archive

git-clean

Xóa untracked files/directories.
Official docs ↗
Cú pháp
git clean [-n|-f|-d]
Hiểu đơn giản
Luôn dry-run bằng -n trước vì file untracked có thể không recover được bởi Git.
git clean -nd
git clean -fd
Đây là lệnh phá hủy dữ liệu untracked.

git-archive

Xuất tree tại commit/tag thành archive không chứa metadata .git.
Official docs ↗
Cú pháp
git archive [--format=zip] <tree-ish>
Hiểu đơn giản
Tiện tạo source package.
git archive --format=zip v1.0.0 > release-v1.0.0.zip