Reputation: 54094
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
?
Upvotes: 4
Views: 7741
Reputation: 54263
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.
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.
Upvotes: 13
Reputation: 26555
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".
Upvotes: -3