git - how to get default branch?

前端 未结 9 2070
轻奢々
轻奢々 2020-12-04 17:44

My team alternates between usage of dev and master as default branch for several repos and I would like to write a script that checks for the default branch when entering a

相关标签:
9条回答
  • 2020-12-04 18:16

    Is there a git command available to determine default branch for remote repository?

    No, there doesn't seem to be:

    git ls-remote -v https://github.com/<user>/<repo>
    

    That would list all branches, but not HEAD (which is the symref which designates the default branch)

    Similarly, the GitHub Reference API can list heads, but would not include HEAD as well.

    0 讨论(0)
  • 2020-12-04 18:22

    There is a --short option to git symbolic-ref. So my preferred command:

    $ git symbolic-ref --short HEAD
    master
    
    0 讨论(0)
  • 2020-12-04 18:25

    This works for me with Git 2.1.10, using a repository cloned from GitHub:

    git branch -r --points-at refs/remotes/origin/HEAD
    

    A major problem with this approach is that it lists every remote branch pointing to HEAD; however, the output includes a hint:

      origin/HEAD -> origin/master
      origin/master
      origin/test123
    

    So you can post-process the output with grep or similar to find the one with the arrow:

    git branch -r --points-at refs/remotes/origin/HEAD | grep '\->' | cut -d' ' -f5 | cut -d/ -f2
    
    0 讨论(0)
提交回复
热议问题