In Git branches are just pointers (references) to commits in DAG (graph) of commits. This means that deleting branch removes only reference to commits, which might make some commits in DAG unreachable, thus invisible. But all commits that were on deleted branch would be still in repository, at least until unreachable commits would get pruned (e.g. using git gc
).
Note that git branch -d
would refuse to delete branch if it cannot be sure that deleting it wouldn't leave unreachable commits. You need to use stronger git branch -D
to force deletion of branch, if it might leave unreachable commits.
Note also that unreachable commits, if they are present, are only those commits between last tip of deleted branch and either the commit that got merged to other existing branch, any tagged commit, or the branching point; whichever later. For example in the following situation:
----O----*----*----/M----* <-- master <-- HEAD
\ /
\--.----.--/--x---y <-- deleted branch
only commits 'x' and 'y' would get unreachable after deleting branch.
If you operated on deleted branch within gc.reflogExpire
period, default 90 days, you would have last tip of deleted branch recorded in HEAD reflog (see git reflog show HEAD
, or git log --oneline --walk-reflogs HEAD
). You should be able to use HEAD reflog to recover delted pointer. Note also that in this case unreachable commits in just deleted branch would be protected from pruning (removing) within gc.reflogExpireUnreachable
period, which by default is 30 days.
If you can't find tip of just deleted branch in reflog for HEAD, you can try to use git fsck
to find "unreachable commit <sha1>", and examine those (via git show <sha1>
or git log <sha1>
) to find tip of deleted branch.
Independent on how you find tip of deleted branch, you can undo deletion, or rather re-create just deleted branch using
git branch <deleted-branch> <found-sha1-id>
Note however that reflog for a branch would be lost.