How do I list all remote branches in Git 1.7+?

2019-01-20 21:07发布

I've tried git branch -r, but that only lists remote branches that I've tracked locally. How do I find the list of those that I haven't? (It doesn't matter to me whether the command lists all remote branches or only those that are untracked.)

15条回答
姐就是有狂的资本
2楼-- · 2019-01-20 21:34

The simplest way I found:

git branch -a
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-20 21:41

The best command to run is git remote show [remote]. This will show all branches, remote and local, tracked and untracked.

Here's an example from an open source project:

> git remote show origin
* remote origin
  Fetch URL: https://github.com/OneBusAway/onebusaway-android
  Push  URL: https://github.com/OneBusAway/onebusaway-android
  HEAD branch: master
  Remote branches:
    amazon-rc2                   new (next fetch will store in remotes/origin)
    amazon-rc3                   new (next fetch will store in remotes/origin)
    arrivalStyleBDefault         new (next fetch will store in remotes/origin)
    develop                      tracked
    master                       tracked
    refs/remotes/origin/branding stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    develop merges with remote develop
    master  merges with remote master
  Local refs configured for 'git push':
    develop pushes to develop (local out of date)
    master  pushes to master  (up to date)

If we just want to get the remote branches, we can use grep. The command we'd want to use would be:

grep "\w*\s*(new|tracked)" -E

With this command:

> git remote show origin | grep "\w*\s*(new|tracked)" -E
    amazon-rc2                   new (next fetch will store in remotes/origin)
    amazon-rc3                   new (next fetch will store in remotes/origin)
    arrivalStyleBDefault         new (next fetch will store in remotes/origin)
    develop                      tracked
    master                       tracked

You can also create an alias for this:

git config --global alias.branches "!git remote show origin | grep \w*\s*(new|tracked) -E"

Then you can just run git branches.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-20 21:43

Git Branching - Remote Branches

git ls-remote

Git documentation.

查看更多
登录 后发表回答