代码之家  ›  专栏  ›  技术社区  ›  James A. Rosen

如何在Git 1.7+中列出所有远程分支?

  •  503
  • James A. Rosen  · 技术社区  · 16 年前

    我试过了 git branch -r ,但这只列出了我在本地跟踪的远程分支。我怎样才能找到我没有的名单呢?(命令是否列出对我来说无关紧要 全部的 远程分支或仅限于未跟踪的分支。)

    16 回复  |  直到 7 年前
        1
  •  628
  •   Andy    8 年前

    对于绝大多数人 〔1〕 这里的游客, the correct and simplest answer to the question “如何列出Git 1.7+中的所有远程分支?”是:

    git branch -r
    

    对于少数人 〔1〕 git branch -r 不起作用。如果 列出远程分支 不工作尝试:

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

    如果 列出远程分支 不起作用,那么也许 Cascabel 说 "you've modified the default refspec, so that git fetch and git remote update don't fetch all the remote 's branches" .


    〔1〕 截至2018年2月的脚注撰写时,我查看了评论,发现 列出远程分支 为绝大多数人工作(约90%或 125 由于 140 )

    如果 列出远程分支 不工作,检查 git config --get remote.origin.fetch 包含通配符( * ) this answer

        2
  •  152
  •   Klas Mellbourn    10 年前

    remote show 显示远程上的所有分支,包括那些未在本地跟踪的分支,甚至那些尚未提取的分支。

    git remote show <remote-name>
    

    它还试图显示分行相对于本地回购的状态:

    > git remote show origin
    * remote origin
      Fetch URL: C:/git/.\remote_repo.git
      Push  URL: C:/git/.\remote_repo.git
      HEAD branch: master
      Remote branches:
        branch_that_is_not_even_fetched new (next fetch will store in remotes/origin)
        branch_that_is_not_tracked      tracked
        branch_that_is_tracked          tracked
        master                          tracked
      Local branches configured for 'git pull':
        branch_that_is_tracked merges with remote branch_that_is_tracked
        master                 merges with remote master
      Local refs configured for 'git push':
        branch_that_is_tracked pushes to branch_that_is_tracked (fast-forwardable)
        master                 pushes to master                 (up to date)
    
        3
  •  46
  •   Idan K    16 年前
    git branch -a | grep remotes/*
    
        4
  •  36
  •   Nick Gronow    9 年前

    使用 git branch -r 列出所有远程分支和 git branch -a 列出本地和远程上的所有分支。不过,这些清单已经过时了。要使这些列表保持最新,请运行

    git remote update --prune
    

    它将用远程的所有新分支更新您的本地分支列表,并删除不再存在的所有分支。在不使用--prune的情况下运行这个更新命令将检索新的分支,但不会删除远程上不再存在的分支。

    您可以通过指定一个远程来加速此更新,否则它将从您添加的所有远程中提取更新,就像这样。

    git remote update --prune origin
    
        5
  •  29
  •   Seakayone    13 年前

    但是

    git branch -ar

    应该这样做。

        6
  •  17
  •   Max Sohrt    12 年前

    你也可以这样做 git fetch 其次是 git branch -r . 如果不提取,您将看不到最新的分支。

        7
  •  17
  •   Ninjakannon Ami Tavory    9 年前

    Git分支-远程分支

    git ls-remote
    

    Git documentation .

        8
  •  9
  •   developer_hatch    9 年前

    我发现最简单的方法是:

    git branch -a
    
        9
  •  5
  •   Anubian Noob    11 年前

    运行的最佳命令是 git remote show [remote] . 这将显示所有分支,远程和本地,跟踪和未跟踪。

    下面是一个开源项目的例子:

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

    如果我们只想得到远程分支,我们可以使用 grep . 我们要使用的命令是:

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

    使用此命令:

    > 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
    

    还可以为此创建别名:

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

    然后你就可以跑了 git branches .

        10
  •  5
  •   simhumileco Janarthanan Ramu    9 年前

    TL;TR;

    这就是您的问题的解决方案:

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

    或:

    git remote update --prune    # To update all remotes
    git branch <TAB>             # To display all branches
    
        11
  •  3
  •   Issam Ressani    8 年前

    使用Gitbash,您可以使用:

    查看所有的分支

        12
  •  0
  •   ThorSummoner    11 年前

    我最后做了一个混乱的shell管道来获得我想要的,只是从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'
    
        13
  •  0
  •   Nayagam    8 年前

    使用此命令,

    git log -r --oneline --no-merges --simplify-by-decoration --pretty=format:"%n %Cred CommitID %Creset: %h %n %Cred Remote Branch %Creset :%d %n %Cred Commit Message %Creset: %s %n"
    
    
    CommitID       : 27385d919 
    Remote Branch  : (origin/ALPHA) 
    Commit Message :  New branch created
    

    列出远程分支引用的所有远程分支,包括提交消息、提交ID。

        14
  •  0
  •   Ashish Mehta    7 年前

    试试这个…

    git fetch origin
    git branch -a
    
        15
  •  -1
  •   user3737174    12 年前

    确保您列出的远程源站确实是您想要的存储库,而不是较旧的克隆。

        16
  •  -2
  •   Jed Schneider    16 年前

    尝试

     git branch -at