I’m working with a Git repository and trying to understand how to git list branches specifically, I want to see all branches, including master, if it exists.
When I run:
bash
Copy
Edit
git branch -a
It only shows:
nginx
Copy
Edit
mongodbutils
Even git show-branch gives:
csharp
Copy
Edit
[mongodbutils] mongodbutils
But then, this command gives a surprising result:
bash
Copy
Edit
git show
sql
Copy
Edit
fatal: your current branch 'master' does not have any commits yet
Also, ls .git/refs/heads/ only lists mongodbutils.
So if I try to git list branches, how do I ensure I see all branches, including master, even if it’s uncommitted or empty?
This is important because I initially assumed master didn’t exist, until I saw that error.
I created the repo using git init and pushed only the mongodbutils branch from another repo.
Would love to know how to correctly list all branches, especially those without any commits.
The key thing to know is that Git only “remembers” branches that have commits.
If your master branch was created during git init but never had a commit, it technically exists, but doesn’t show up in git branch or ls .git/refs/heads/ because there’s no object for it to point to yet.
In this case, try:
bash
Copy
Edit
git symbolic-ref refs/remotes/origin/HEAD
or
bash
Copy
Edit
cat .git/HEAD
This will show you where HEAD is pointing, even if that branch doesn’t have commits.
It’s how I confirmed a “phantom” master existed in one of my repos after an empty init + remote push.
If you’ve pushed only mongodbutils from another repo and never committed anything to master, Git won’t list master because it’s just a symbolic ref without an actual commit.
It won’t appear in git branch -a or show-branch.
You can verify orphan branches (i.e., those that exist logically but aren’t initialized) by trying:
bash
Copy
Edit
git rev-parse --verify master
If it returns an error, it means the branch name exists (maybe in HEAD), but there’s no commit history tied to it.
It’s Git’s way of saying “this branch is on paper but not in practice.”
@MattD_Burch This happens more often than you’d think, especially when creating a repo locally, then pushing only one branch (mongodbutils) from elsewhere.
If master has never been committed to, Git doesn’t treat it like a “real” branch.
One trick that’s worked for me: just create an empty commit on master so it becomes a full-fledged branch:
bash
Copy
Edit
git checkout --orphan master
git commit --allow-empty -m "Initialize master"
Now run git branch -a and you’ll see it show up.
It’s a clean way to make the master branch visible without needing to stage files or modify history.