1

I assume that the name master branch which is the "main" branch that everyone is working in a git repository,is just a convention. Right?
So how can I know what is the "main" branch name in a git repository that has overriden so as not to use the name master?

Cratylus
  • 52,998
  • 69
  • 209
  • 339

2 Answers2

6

Assuming you've already cloned the repository and the name of the remote is origin, then you can run the following:

git symbolic-ref refs/remotes/origin/HEAD

This will usually print refs/remotes/origin/main or refs/remotes/origin/master, but if the project chose someotherbranch as their default it'll show refs/remotes/origin/someotherbranch.

Some background: In a local (non-bare) repository, HEAD indicates what is currently checked out. In a shared (bare) repository, there is no working directory so there is nothing checked out. Thus, in bare repositories, HEAD is used to indicate the default branch for the repository (the branch you get when you run git clone <url>). When you clone a repository, the remote repository's HEAD reference is copied to your local refs/remotes/origin/HEAD reference.

Caveats

  • If the remote HEAD reference does not point to a valid commit (for example, you are cloning a newly created empty repo), git clone will not create origin/HEAD.
  • git remote update and git fetch do not update origin/HEAD to match the current value of the remote HEAD reference. (You can run git remote set-head origin -a to update or create origin/HEAD, assuming the remote repository's HEAD points to a valid commit.)

I would argue that both of these behaviors are bugs. However, I believe older versions of Git (circa 2013) updated origin/HEAD to match the remote, which would mean that the Git devs intentionally changed the behavior. If that is correct, presumably there was a good reason for the change.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
  • Won't this print the current `HEAD`? Couldn't the `HEAD` be in *any* branch? – Cratylus Jun 14 '13 at 05:50
  • @Cratylus: That command prints the value of `origin/HEAD`, not `HEAD` (note that `refs/remotes/origin/HEAD` is a different reference from `HEAD`). Yes, someone could change the name of the remote repository's default branch at any time, but it's very rare. See my updated answer. – Richard Hansen Jun 14 '13 at 06:04
  • 1
    I just get `fatal: ref refs/remotes/origin/HEAD is not a symbolic ref` (on `git version 2.36.2`). Tried on multiple different repository clones. – akaihola Jan 07 '23 at 22:29
  • 1
    @akaihola I updated the answer with some caveats. – Richard Hansen Jan 08 '23 at 04:33
  • In versions of git where this no longer works, it looks like [this answer](https://stackoverflow.com/a/50056710/2071807) will do the job. – LondonRob May 02 '23 at 14:24
0

Yes, master is just a convention. Not every repository has a branch called master. Also git allows you arbitrary work flows. Different projects use different branching strategies. There might be something like a "main branch", but sometimes there is simply no need for it.

You can always use git branch -m to give a commit a new name and git reset to give a name a new commit.

Most of the time you can use the name of the branch to get an idea what it includes. Otherwise can use something like gitk to have a look at your commit graph. This might help you to recognize something like a "main branch".

michas
  • 25,361
  • 15
  • 76
  • 121
  • `git log --decorate --graph --oneline` gives a fairly usable text-only approximation of the gitk graph visualization. – Ryan Stewart Jun 14 '13 at 04:13