Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you delete a git branch do it's commits go with it?

Tags:

git

If I create a new branch, make a few commits, and then decide, "wow, I've done nothing good here," and I delete the branch (git branch -d branchname) do those few commits go away too? As in, do they disappear from my git log?

like image 707
Costa Michailidis Avatar asked Oct 16 '25 19:10

Costa Michailidis


2 Answers

The commits won't show up in git log anymore when no branch references them, but they will be kept around for a while to prevent accidental loss of work. There are several safety layers:

  • Commits are only ever considered for garbage collection if they are no longer referenced. This typically happens if only a single branch/ref had a reference to them, and you delete that ref.
  • Unless configured differently, Git retains a log of old states of a branch ("reflog"). Reflog entries are expired eventually, subject to a configurable age threshold (two, in fact: one for reflog entries that contain stuff that's reachable from the current state of the branch, and one for stuff no longer reachable from the branch; the default for the latter is 30 days). Before this, git gc will keep your reflog entries there and will not delete commits still reachable via those reflog entries.
  • Even if you forcibly clear your reflogs, git gc will still keep objects if they were created less than two weeks ago (by default; this is configurable).
  • After all that, the objects are physically deleted when you run git gc.

To look at reflogs, try git log -g SOMEBRANCH.

like image 103
Jan Krüger Avatar answered Oct 18 '25 11:10

Jan Krüger


Yes, they disappear from the git log, and no, they do not disappear from the repository until the subsequent git gc (garbage collector) command.

You can still find those commits using the git reflog command.

like image 32
SzG Avatar answered Oct 18 '25 09:10

SzG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!