2

Do I need to run a git fetch to see all remote branches with git branch -r ?

Or in other words: does a git branch -r execute a remote call, or only the known remote branches are displayed?

TheAnonymousModeIT
  • 777
  • 1
  • 6
  • 18
  • 6
    No remote call when calling `git branch -r`. so **yes**, you need to fetch to be able to see new branches (or movement of branches). – eftshift0 May 02 '23 at 06:37
  • 1
    Disable your network connection then run various Git commands to learn which of them contacts the remote repository and which do not. `fetch`, `push`, `pull`, `ls-remote` and probably a couple more need to contact the remote to do their job. Most Git commands operate only on the local repository. – axiac May 02 '23 at 07:02

3 Answers3

2

Yes, but check the refspec used for your remote:

git config --get remote.origin.fetch

You should see:

+refs/heads/*:refs/remotes/origin/*

That would fetch all remote branches.

A refspec like +refs/heads/dev:refs/remotes/origin/dev would only fetch and update the 'dev' remote tracking branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

git branch does not perform any network requests and will only show the currently known state of your local remote-tracking branches.

But it is also possible to view the branches of a remote repository with git ls-remote --heads, which performs a network request without updating your local remote-tracking branches.

knittl
  • 246,190
  • 53
  • 318
  • 364
0

git branch -r doesn't execute a remote call, it only lists the remote tracking branches you have locally. (see the folder ´.git/refs/remotes´)

Note that git branch -r also will list a lot of remote branches which haven't existed for quite a long time unless you add the --prune argument to you pull and fetch commands. (or you could change the settings to always prune when fetching.)

Kim
  • 397
  • 1
  • 10