Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Finding the primary branch of a Git Repository

If you’re using Git, you might have noticed that the primary/default branch of a Git repository is called master. But, since the word master has a negative connotation, many organizations have started to use different names for the default branch.

For instance, GitHub has renamed the default branch from master to main and GitLab has renamed it to default.

And if you’re working with multiple Git repositories, it can be hard to remember the default branch name of each repository. So, how do you find out the default branch of a Git repository?

Well, Liam Hammett has recently shared a neat Git command that will let you do just that.

git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4

As you can tell, the git symbolic-ref command will return the symbolic reference of the HEAD of the remote origin and the cut command will cut the output of the git symbolic-ref command and return the 4th field which is the name of the default branch.

Now, if you want to make this command more accessible, you can create a Git alias for it like so.

git config --global alias.default-branch '!git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4'

And now, you can use this alias like so.

git default-branch

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?