Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "git branch" silent in new repositories?

Tags:

git

git-branch

When you create a new repository and run git branch, it exits silently. For example:

$ mkdir /tmp/foo; cd /tmp/foo; git init
Initialized empty Git repository in /tmp/foo/.git/

$ git branch

Why doesn't the command provide any output, or show the master branch?

like image 513
Todd A. Jacobs Avatar asked Jan 26 '26 20:01

Todd A. Jacobs


1 Answers

TL;DR

No branch heads exist yet.

Detailed Explanation

A Git repository has no branches until you make your first commit. A newly-initialized repository sets HEAD to refs/heads/master, but refs/heads/master won't exist or contain a commit pointer until after the first commit is made.

During a commit, Git dereferences the symbolic-ref HEAD to find the head of the current branch, and then updates that head with the commit hash supplied by git-commit-tree.

The end result is that git branch has nothing to report in a new repository. With no branch heads present, it simply terminates silently with an exit status of zero.

See Also

  • git-branch(1)
  • git-commit-tree(1)
  • git-symbolic-ref(1).
  • git-update-ref(1)
  • gitcore-tutorial(7)
like image 77
Todd A. Jacobs Avatar answered Jan 28 '26 15:01

Todd A. Jacobs



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!