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:
git reflog --all | grep "feature/deleted-branch"
# Output: abc123 refs/heads/feature/deleted-branch@{0}: commit: feat: last work
# Recreate branch tại commit đó:
git checkout -b feature/deleted-branch abc123
# hoặc:
git branch feature/deleted-branch abc123Nếu không nhớ tên branch:
git reflog | grep "checkout: moving from"
# Tìm lần cuối bạn checkout khỏi branch đóNếu reflog không có (branch trên remote bị xóa):
git fetch origin # nếu remote còn lưu
# Hoặc hỏi teammate — họ có thể có local copyNếu đã git gc chạy:
git fsck --lost-found
# Tìm "dangling commit" — đó là commits không còn branch nào trỏ đến
for sha in .git/lost-found/commit/*; do git log --oneline -1 $sha; done # xem commits tìm đượcPhò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>.
A branch is just a pointer to a commit. Deleting a branch does not delete commits — only the pointer. Commits still exist in the git object store until git gc runs.
Recovery via reflog:
git reflog --all | grep "feature/deleted-branch"
# Output: abc123 refs/heads/feature/deleted-branch@{0}: commit: feat: last work
# Recreate the branch at that commit:
git checkout -b feature/deleted-branch abc123
# or:
git branch feature/deleted-branch abc123If you do not remember the branch name:
git reflog | grep "checkout: moving from"
# Find the last time you checked out of that branchIf reflog does not have it (remote branch deleted):
git fetch origin # if remote still has it
# Or ask a teammate — they may have a local copyIf git gc has already run:
git fsck --lost-found
# Find "dangling commit" entries — commits no branch points to
for sha in .git/lost-found/commit/*; do git log --oneline -1 $sha; done # inspect found commitsPrevention: before deleting a branch, push to remote or tag the last commit: git tag backup/feature-before-delete <branch>.