Trung BìnhGit iconGit

`git stash` nâng cao: push, pop, apply, list, show, drop — những pattern thực tế cần biết?

Cơ bản hay bị hiểu nhầm:
- git stash = git stash push — stash working dir + index
- pop = apply + drop (xóa stash sau khi apply)
- apply = apply nhưng GIỮ stash (dùng khi muốn apply vào nhiều branches)

Patterns thực tế:

bash
# Stash có tên (dễ nhận dạng):
git stash push -m "WIP: cart refactor — half done"

# Stash kể cả untracked files:
git stash push -u  # --include-untracked

# Stash chỉ 1 file (non-interactive):
git stash push -- src/cart.ts  # pathspec — stash chính xác 1 file
# Hoặc interactive patch (chọn từng hunk):
git stash push -p  # -p mở interactive hunk selection, không phải file filter đơn thuần

# Xem nội dung stash trước khi apply:
git stash show -p stash@{2}  # show diff

# Apply stash cụ thể (không nhất thiết top):
git stash apply stash@{2}

# Xóa stash cụ thể:
git stash drop stash@{2}

# Apply vào branch mới (tạo branch từ stash):
git stash branch feature/new-branch stash@{0}

Pitfalls:
- Stash không track untracked files mặc định → dùng -u
- Stash conflict khi apply: resolve như merge conflict, sau đó git stash drop thủ công
- Stash list không hiển thị branch name mặc định → luôn đặt tên với -m

Thực tế: nhiều devs lạm dụng stash thay vì commit WIP. Commit WIP với feat(wip): prefix tốt hơn — có history, không bị mất.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git