git - how to get default branch?

前端 未结 9 2212
轻奢々
轻奢々 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:08

    This question is a bit old but in case anyone comes across this more recently...

    git remote show  | awk '/HEAD branch/ {print $NF}'
    

    That will also only display the branch name, not including any of the whitespace or other nonsense.

    I like to save this using a couple git aliases (I have a bunch of useful aliases like this):

    upstream-name = !git remote | egrep -o '(upstream|origin)' | tail -1
    head-branch = !git remote show $(git upstream-name) | awk '/HEAD branch/ {print $NF}'
    

    I use "upstream" and "origin" as my remotes almost 100% of the time ("upstream" when I go with a Fork & Pull workflow... which is often). Your use case may not need the upstream-name alias, I just find it useful.

提交回复
热议问题