git - how to get default branch?

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

提交回复
热议问题