Why doesn't git branch command show any branches?

87 Views Asked by At

Few months ago I was going through a tutorial teaching git. There was the main branch (obviously) and then I created a second branch.

Now I'm back at learning git and when I try to see the branches available using git branch I don't get anything. Not even a main branch which is there by default.

What could be the problem?

enter image description here

Thanks in advance

2

There are 2 best solutions below

3
Guildenstern On

There is no context but I’m assuming that you are in a fresh repository.

git init

Yes, if you then do this you will get no output:

git branch

Because you are on an “unborn branch”; you haven’t created any commits yet so there is no branch to point to.

If you however create a commit:

touch a.txt
git add a.txt
git commit -mInit

Then you will see the branch:

$ git branch
* main

Because the branch has been born (it points to something).

0
Envy On

You need to make at least one commit for the git branch command to list branches. Use the following command to make a commit. And then run git branch.

git commit -a -m "The Commit message"

This will show all the branches, that are remote or local:

git branch -av

Or if you don't have the branch locally, you can run:

git fetch --all

And if none of that works, I recommend you to read this documentation on GeeksForGeeks.