I used to think git branch -r
was the way to go for finding remote branches.
Until today, when I was working on repository I hadn’t updated locally for a while and doing git branch -r
gave me some older branch names that I was pretty sure I had deleted (and double checking GitHub confirmed that too).
1 2 3 4 5 |
PS> git branch -r origin/0.1 origin/master origin/testing origin/testing2 |
Moreover shouldn’t git branch -r
prompt for my SSH key when connecting to the repository? It doesn’t, so maybe the command’s just returning some cached info?
Turns out the accurate way of querying a remote repository is to use the git ls-remote
command:
1 2 3 4 5 6 7 |
PS> git ls-remote Enter passphrase for key '/c/.ssh/id_rsa': From git@github.com:rakheshster/PS-AppMgmt.git 974390363db7e110deff9a84a774b363714ffe6e HEAD 974390363db7e110deff9a84a774b363714ffe6e refs/heads/0.1 974390363db7e110deff9a84a774b363714ffe6e refs/heads/master 974390363db7e110deff9a84a774b363714ffe6e refs/heads/testing |
This does show the correct branches.
A better way to do this is to use the git remote
command. This is easier to remember too and the output is more informative:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
PS> git remote show origin Enter passphrase for key '/c/.ssh/id_rsa': * remote origin Fetch URL: git@github.com:rakheshster/PS-AppMgmt.git Push URL: git@github.com:rakheshster/PS-AppMgmt.git HEAD branch (remote HEAD is ambiguous, may be one of the following): 0.1 master testing Remote branches: 0.1 tracked master tracked refs/remotes/origin/testing2 stale (use 'git remote prune' to remove) testing tracked Local branch configured for 'git pull': master merges with remote master Local refs configured for 'git push': master pushes to master (local out of date) testing pushes to testing (up to date) |
This command actually runs the git ls-remote
command behind the scenes. If you want to see the cached information instead (what git branch -r
was looking at) use the -n
switch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
PS> git remote show origin -n Enter passphrase for key '/c/.ssh/id_rsa': * remote origin Fetch URL: git@github.com:rakheshster/PS-AppMgmt.git Push URL: git@github.com:rakheshster/PS-AppMgmt.git HEAD branch: (not queried) Remote branches: (status not queried) 0.1 master testing testing2 Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push' (status not queried): (matching) pushes to (matching) |
To remove the branches that are no longer present locally (stale branches) use the git remote prune
command:
1 2 3 4 5 |
PS> git remote prune origin Enter passphrase for key '/c/.ssh/id_rsa': Pruning origin URL: git@github.com:rakheshster/PS-AppMgmt.git * [pruned] origin/testing2 |
To summarize:
- View current remote branches via
git remote show origin
- Prune stale local branches via
git remote prune origin
Of course replace origin
appropriately if your remote has a different name. The primary remote repository is usually given a friendly name origin
.