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ế:
# 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}Lưu ý:
- 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.
Basics often misunderstood:
- git stash = git stash push — stashes working dir + index
- pop = apply + drop (removes stash after applying)
- apply = apply but KEEP the stash (use when applying to multiple branches)
Real-world patterns:
# Named stash (easy to identify):
git stash push -m "WIP: cart refactor — half done"
# Stash including untracked files:
git stash push -u # --include-untracked
# Stash only one file (non-interactive):
git stash push -- src/cart.ts # pathspec — stashes exactly one file
# Or interactive hunk selection (not a simple file filter):
git stash push -p # -p opens interactive patch mode for selecting hunks
# Inspect stash content before applying:
git stash show -p stash@{2} # show diff
# Apply a specific stash (not necessarily the top):
git stash apply stash@{2}
# Delete a specific stash:
git stash drop stash@{2}
# Apply into a new branch (create branch from stash):
git stash branch feature/new-branch stash@{0}Pitfalls:
- Stash does not track untracked files by default → use -u
- Stash conflict on apply: resolve like a merge conflict, then git stash drop manually
- Stash list does not show branch names by default → always name with -m
In practice: many devs overuse stash instead of committing WIP. Committing WIP with a feat(wip): prefix is better — you get history, you cannot lose it.