When a git branch is deleted, do its commits also get removed?



In the event that I create a new branch, make several commits, and later realize that my work is unsatisfactory, if I were to delete the branch (

git branch -d branchname

), would those specific commits also be removed? In other words, would they no longer be visible in my git log?

Solution 1:

The commits will no longer appear in

git log

if no branch references them, but they will be retained for a period of time to avoid unintended loss of work. Multiple safety layers are in place.

  • Commits are evaluated for garbage collection solely when they are no longer referenced, which typically occurs when a single branch/ref is the only reference and is subsequently deleted.
  • By default, Git maintains a log of previous states of a branch called “reflog”. Eventually, these reflog entries expire based on a configurable age threshold. There are two thresholds: one for reflog entries that contain content reachable from the current branch state, and another for content that is no longer reachable from the branch (with a default of 30 days). Prior to expiry, the “msdt_code1” command will retain your reflog entries and ensure that commits reachable through those entries are not deleted.
  • In the event that you choose to clear your reflogs forcefully,

    git gc

    will retain objects created within the past two weeks, as per the default configuration.
  • Once all the necessary actions have been completed, the objects are removed from the physical space upon the execution of

    git gc


To view reflogs, attempt executing the command

git log -g SOMEBRANCH


Solution 2:

Indeed, while they are removed from the git log, they remain in the repository until the following

git gc

garbage collector) command


git reflog

command allows you to locate those commits.

Frequently Asked Questions

Posted in Git