Branch chỉ là pointer đến commit. Xóa branch không xóa commits — chỉ xóa pointer. Commits vẫn tồn tại trong git object store cho đến khi git gc chạy.
Recovery qua reflog:
bash
git reflog --all | grep "feature/deleted-branch"
# abc123 refs/heads/feature/deleted-branch@{0}: commit: feat: last work
git branch feature/deleted-branch abc123 # recreate branch tại commit đó- Không nhớ tên branch:
git reflog | grep "checkout: moving from"— tìm lần cuối bạn checkout khỏi branch đó. - Branch trên remote bị xóa:
git fetch originnếu remote còn lưu, hoặc hỏi teammate — họ có thể còn local copy. - Đã
git gcchạy:git fsck --lost-foundliệt kê dangling commits (không còn ref nào trỏ đến) vào.git/lost-found/commit/— duyệt từng sha bằnggit log --oneline -1 <sha>.
Phòng tránh: trước khi xóa branch, push lên remote hoặc tag commit cuối: git tag backup/feature-before-delete <branch>.