Git list of branch names of specific remote

前端 未结 3 920
遥遥无期
遥遥无期 2021-01-31 08:15

How can it possible to get all names of some remote origin branches?

I started from --remote --list options, but got redundant origin/HEAD -> origi

3条回答
  •  不要未来只要你来
    2021-01-31 08:57

    It's important that it should be done without grep, sed, tail or even ghc -e wrappers, only with true git power, because of their unsafeness and variation.

    That is only true for git porcelain commands (see "What does the term porcelain mean in Git?")

    Use the plumbing command ls-remote, and then you will be able to filter its output.

    ls-remote without parameter would still list the remote HEAD:

    git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote origin
    8598d26b4a4bbe416f46087815734d49ba428523    HEAD
    8598d26b4a4bbe416f46087815734d49ba428523    refs/heads/master
    38325f657380ddef07fa32063c44d7d6c601c012    refs/heads/test_trap
    

    But if you ask only for the heads of said remote:

    git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin
    8598d26b4a4bbe416f46087815734d49ba428523    refs/heads/master
    38325f657380ddef07fa32063c44d7d6c601c012    refs/heads/test_trap
    

    Final answer:

    git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin  | sed 's?.*refs/heads/??'
    master
    test_trap
    

    (Yes, it uses sed, but the output of a plumbing command is supposed to be stable enough to be parsed)


    See also Git 2.23 (Q3 2019) which documents an example:

    git branch -r -l '/'
    git for-each-ref 'refs/remotes//'
    

提交回复
热议问题