git – how to get default branch?

I found a way to detect the default-branch if it is not master.

git remote show [your_remote] | sed -n '/HEAD branch/s/.*: //p'

I tested it with multiple repo from gitlab, and it worked fine.
(for the most situations [your_remote] will be origin, run git remote to check the name of your remote)

Leave a Comment