Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show all the branches in a repository?

Tags:

git

I have a Git repository. How can I show all its branches?

Are the following two commands supposed to show all the branches? If yes, why do they not show branch master? I was under the impression that the repository doesn't have branch master, when asking https://stackoverflow.com/questions/54160690/will-pushing-a-new-branch-to-another-repository-create-an-upstream-branch-and-re.

$ git branch -a
  mongodbutils

$ git show-branch
[mongodbutils] mongodbutils

$ ls .git/refs/heads/
mongodbutils

Why does the following command mention branch master?

$ git show
fatal: your current branch 'master' does not have any commits yet

How can I show all the branches (including master in this case) in a repository? I think it is important, or else, I will have the same false impression.

Note: the repository was created by git init an empty directory, and then I git push a branch mongodbutils from another branch into this branch.

like image 979
Tim Avatar asked Jan 12 '19 16:01

Tim


People also ask

How do I get a list of branches on a remote?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands. If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge .

How do I get branches from remote repository?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How many branches are there in a repository?

Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.


1 Answers

As the documentation of git branch explains, git branch --all (or -a) lists all the branches from the local repository, both the local and the remote tracking branches.

A Git branch is just a pointer to a commit. A new repository (just created with git init) does not contain any commits. The current branch on a new repo is master but the master branch does not actually exist. In fact, a new repository does not contain any branch.

The master branch is created when the first commit is created. Or when it is pulled from a remote repository.

Since you didn't create any commit and also didn't pull the master branch, it does not exist in your repo. Both commands you listed show you this thing.

git branch -a is the one you want to use to list the branches.

git show-branch is designed to be used by scripts and GUI tools.

like image 68
axiac Avatar answered Nov 11 '22 06:11

axiac