Question

It seems that we could use

git branch -r

or

git branch -a

to see remote branches. However, when I git clone my repo on GitHub, and then added a branch on GitHub, and tried the commands above, I can't see the new branches on my local machine. I even edited a file and committed on the new branch, and still wasn't able to see the new branch on the local machine using those commands.

Until I used a git pull, then a git branch -a will show that new branch (but a git branch will not). But I think I might not want to use a git pull as it will update my files locally. In that case, can I see all remote branches?

(also, I will see the new branch as remotes/origin/wah-la-la by using git branch -a, and then only after I do a git checkout wah-la-la, then from this point on, I will be able to see both master and wah-la-la when I do a git branch. Can't I see wah-la-la before I do a checkout?)

Was it helpful?

Solution

git branch will only show the branches that exists locally in your local repository (either direct branches, or remote branches). To update those, you can use git fetch and get all the current objects from the remote.

You can also use git ls-remote to list all branches of a remote without fetching anything from it.

OTHER TIPS

I have grown accustomed to split a pull into fetch and merge, in case of the master branch:

git fetch origin
git log --graph --decorate --pretty=oneline --abbrev-commit --all
git merge origin/master

The log command is obviously abbreviated into an alias

When you execute git branch -a, it only shows the local and remote-tracking branches that were already fetched from the remote repository. First you need to get the updates from the remote repository and then you will be able to list new remote-tracking branches:

git remote update
git branch -a

git branch is designed to be fast, every time you run it. If it has to query the network to check for new branches that others might have added, that would make it slow. And it would be bad separation of responsibilities: there are other commands to perform network operations.

To update the list of branches for a specific remote, for example origin, do a git fetch:

git fetch origin

After that you will see the new branches with git branch -r.

To update the remote branches of all remotes:

git remote update
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top