Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Able to recover a deleted branch using git tag

Tags:

git

I have a test repository with a master and test branch. I did make few changes in test branch and did a tagging of the test branch. Then I deleted the test branch (no, I did not merge with master). Surprisingly, when I tried to checkout using the tag which I did earlier, I was able get the test branch which I deleted earlier (of course in DETACHED HEAD state, in "no branch"). How this is possible? Can someone help me understand this??

like image 568
Shunya Avatar asked Dec 25 '22 21:12

Shunya


1 Answers

Git objects are not immediately deleted after operations (for example, they can be recovered with git reflog). You have to explicitly use git gc (which calls git prune too) to manually delete them, or wait an amount of time controlled gc.pruneexpire to automatically prune them (if that's enabled by gc.auto).

Besides, objects are permanently deleted only when there's no reference to them, and in this case, you still have a Tag on it, so the commit and its history is preserved.

Check this command output:

git log --oneline --decorate --graph --all

It will give you an overview of your repository, the commits you see here won't be permanently deleted by garbage collector.

like image 186
KurzedMetal Avatar answered Jan 10 '23 06:01

KurzedMetal