sotmot
sotmot

Reputation: 1313

Change the default branch name in GitHub from main to master globally

With the recent change in the default branch naming convention from master to main in GitHub, I get an error after every first I push to a new repository (I then create a master branch, make it default and then delete the main branch). I do realize that a new branch main can be created after each git init, but, I would prefer to not create a branch each time.

Hence, instead of creating a branch each time, is it possible to change the initial default branch to master across all repositories (globally for a single user)?

Edit:

The commands used are as follows:

git init
git remote add origin <repo-url>

The following doesn't work

git pull

I get:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

However, immediately after a git init, doing git branch -M main creates a main branch and the above problem can be averted. But, I would prefer not to create the same. Instead, can the default branch name be changed globally?

Upvotes: 6

Views: 3320

Answers (1)

Schwern
Schwern

Reputation: 164809

If you made a repo with an initial commit, don't init and add the remote. This can result in a repository which does not match the remote, as you're experiencing. Clone it and use main.

git clone <repo-url>

You can change the Github default in Settings -> Repositories.

Since Git 2.28, you can change your default branch with init.defaultBranch.

[init]
        defaultBranch = main

If you have scripts which assume that master is the remote default, you can query the remote default branch.


If you made a new, empty repository with a master branch, push your master branch. It will be the default.

$ mkdir test123
$ cd test123
$ echo "# test123" >> README.md
$ git init
Initialized empty Git repository in /Users/schwern/tmp/test123/.git/
$ git add README.md
$ git commit -m "first commit"
[master (root-commit) ea90a25] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ git remote set-url origin [email protected]:schwern/test123.git
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 227 bytes | 227.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:schwern/test123.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Upvotes: 4

Related Questions