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:27

Using git branch -r lists all remote branches and git branch -a lists all branches on local and remote. These lists get outdated though. To keep these lists up-to-date, run

git remote update --prune

which will update your local branch list with all new ones from the remote and remove any that are no longer there. Running this update command without the --prune will retrieve new branches but not delete ones no longer on the remote.

You can speed up this update by specifying a remote, otherwise it will pull updates from all remotes you have added, like so

git remote update --prune origin
查看更多
beautiful°
3楼-- · 2019-01-20 21:27

You also may do git fetch followed by a git branch -r. Without fetch you will not see the most current branches.

查看更多
▲ chillily
4楼-- · 2019-01-20 21:27

TL;TR;

This is the solution of your problem:

git remote update --prune    # To update all remotes
git branch -r                # To display remote branches

or:

git remote update --prune    # To update all remotes
git branch <TAB>             # To display all branches
查看更多
beautiful°
5楼-- · 2019-01-20 21:27

I ended up doing a mess shell pipeline to get what I wanted, just merged branches from the origin remote:

git branch -r --all --merged \
    | tail -n +2 \
    | grep -P '^  remotes/origin/(?!HEAD)' \
    | perl -p -e 's/^  remotes\/origin\///g;s/master\n//g'
查看更多
Root(大扎)
6楼-- · 2019-01-20 21:27

try

 git branch -at
查看更多
\"骚年 ilove
7楼-- · 2019-01-20 21:33

For the vast majority[1] of visitors here, the correct and simplest answer to the question "How do I list all remote branches in Git 1.7+?" is:

git branch -r

For a small minority[1] git branch -r does not work. If git branch -r does not work try:

git ls-remote --heads <remote-name>

If git branch -r does not work, then maybe as Cascabel says "you've modified the default refspec, so that git fetch and git remote update don't fetch all the remote's branches".


[1] As of the writing of this footnote 2018-Feb, I looked at the comments and see that the git branch -r works for the vast majority (about 90% or 125 out of 140).

If git branch -r does not work, check git config --get remote.origin.fetch contains a wildcard (*) as per this answer

查看更多
登录 后发表回答