1

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?)

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Just remember, the key to understanding git is that it's just a tree of commits, with movable Post-it notes (references) attached to the commits. Also, are you aware of the [Pro Git book](http://git-scm.com/book)? It's free. See my profile for chapter recommendations, as well as other git resources. –  May 11 '14 at 21:34
  • Related: [Track a new remote branch created on GitHub](http://stackoverflow.com/q/11262703/456814). –  Jun 01 '14 at 02:23

4 Answers4

5

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • `git ls-remote` will do the job – nonopolarity May 11 '14 at 19:50
  • @動靜能量 people usually use `git fetch`. It's less typing, and will grab all of the objects and store them locally. `git ls-remote` won't save any objects for you, so you end up making more network calls in the long-run. –  May 11 '14 at 21:27
2

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
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • i think git is starting to wreak my brain as to why `git branch -r` (for remote) doesn't list the remote branches but that I have to "update" the remote by `git remote update` – nonopolarity May 11 '14 at 19:33
  • To be more precise, `-r` is not to list remote branches, it's to list remote-tracking branches. Git maintains a list locally about local and remote-tracking branches. `git branch` uses this list, it doesn't communicate with the remote repository. That's why you need get the updates from the remote repository first to update this local list of branches. – Gergo Erdosi May 11 '14 at 19:39
  • `git remote update` is the old way to update your repo, the new preferred way (since Git 1.6.6) is simply `git fetch`. –  Jun 01 '14 at 02:25
2

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

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
2

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
janos
  • 120,954
  • 29
  • 226
  • 236
  • Please note that `git fetch --all` will also fetch from all of your configured remotes. –  Jun 01 '14 at 02:26