31

One of the things github now recommends is changing the branch to main instead of master.

The code given on the github site is:

git branch -M main

That never works for me, so I thought I would mention it here. I have a hard time believing this problem only occurs with me.

error: refname refs/heads/master not found
fatal: Branch rename failed
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22

4 Answers4

23

Update: The -m and -M options to git branch were upgrade in Git 2.30 to allow renaming the not-yet-existing current branch name in special situations, such as when you're in a new, empty repository.


You mention in your own answer that git branch -m main (or the same with -M) only works once you have an initial commit.

Alternatively, before creating any commits, use git checkout -b main to switch the name of the unborn branch to main.

There is no functional difference between creating the initial commit, then renaming the branch, vs changing the unborn branch name, then making the initial commit. Commits do not remember which branch was the current branch when they were made,1 so you're free to change branch names at any time. (Other people remember branch names, in their brains, and may have saved some branch names in clones, so it's best to do all these name changes before anyone else gets hold of these names. But that's outside your own Git.)


1The git merge command does, however, generate a default merge message:

merge branch X [into Y]

and git pull generates a default merge message:

merge branch X of 'url' [into Y]

where X is the argument you gave to git merge—with a URL added when using git pull to run git merge—and Y is present, and is the name of the current branch, if the current branch is not the designated "special" branch. This was hardcoded as master in the past, but is becoming configurable. The end result of all of this is that you tend to get messages of the form merge branch feature when merging features into master/main, and messages of the form merge branch feature into develop when merging features into other branches.

Note that these autogenerated messages convey relatively little useful information, especially if you delete the feature branch after merging it. To take a particular example, suppose you reserve the name hotfix for a temporary branch on which hot-fixes are made. Then your repository will have the occasional "merge branch hotfix" commit, but each of these messages is for a different hotfix. The information being conveyed here is nearly useless—you need the date of the merge, not just the message, to find the right "hot bug". At worst, it may be worse than useless since it might send you looking at the wrong "hot bug". If you manually replace this with "merge fix for critical customer bug #1234", you get a useful message.

(If your branch names include the bug-reference-number, then these messages are useful. The "into branch Y" part, which uses the current branch, still seems quite marginal to me, though.)

nvidot
  • 1,262
  • 1
  • 7
  • 20
torek
  • 448,244
  • 59
  • 642
  • 775
  • this is a great explanation but when I was doing that in like it said to by following the steps, it doesn't recognize the word branch – AlThePal78 Oct 02 '21 at 20:42
  • @daddycardona: I'm afraid I'll need to see a cut-and-paste of your Git command line session to know what you mean here. – torek Oct 02 '21 at 21:30
  • @torek I figured it out I wrote some stupid little mistakes and figured out how to do it and make the keys to commit it and everything but thanks for responding :) – AlThePal78 Oct 10 '21 at 23:20
5

There has to be at least one commit for this to work.

git status
On branch master

No commits yet


Do the first commit.

git add *.html
git commit -m 'first'
[master (root-commit) 455481e] first
 1 file changed, 54 insertions(+)
 create mode 100644 start.html
git branch -m master main
git status
On branch main

Note a more complete explanation at How do I rename a local Git branch?

Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22
5

GitHub uses "main" as the name of your primary branch. It used to use "master", but git still defaults to "master". You want to make sure that both git and GitHub are using the same branch name, so use "main". The best way to accomplish this is to change your default branch name to "main":

$ git config --global init.defaultBranch main
rafazi
  • 51
  • 1
  • 3
2

It is important to point out that the only reason git creates an initial branch called master is due to the config setting init.defaultbranch set during installation of git-scm. If left to the default Let Git decide it will be master. If you select the option Override the default... and you'd specified something else, for example the preset main, this will be the system wide default. Select this and you can skip the step to name the initial branch during repo init.

Check System setting

You can check the value of the system setting with:

git config --system init.defaultbranch

...returning the system wide set value.

Override the system setting

You can override a setting on a global, user, level with:

git config --global --add init.defaultbranch mistress

or, on a per project level with:

git config --add init.defaultbranch mastress

Alternatively you could go and manually change the gitconfig file which—in windows—is stored with the programm in the etc folder. Locate the init.defaultbranch line and edit accordingly.

theking2
  • 2,174
  • 1
  • 27
  • 36