5

With the recent change of master to main in Github, it seems I need to first push to master and then merge with main. When I try push using the --remote-upstream to main I get an error: error: src refspec main does not match any.

On iterm2 with OMZ, when I first run git init I see master in the path.

How do I push directly to main instead of master first?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
electrophile
  • 285
  • 1
  • 5
  • 16

2 Answers2

6

If I recall correctly, in Git version 2.28.0, you can use the command git init -b in order to set up your default branch.

For example, to create a new repository with main branch as the default branch.

git init -b main

If you like to type longer names:

git init --initial-branch=main

You can even set it globally.

git config --global init.defaultBranch main

As an alternative, when creating a new repository, you can just do git clone to your system. The default branch will be adjusted accordingly.


About git init -b command, taken from Git's documentation:

Use the specified name for the initial branch in the newly created repository. If not specified, fall back to the default name (currently master, but this is subject to change in the future; the name can be customized via the init.defaultBranch configuration variable).

Read more here.


Another alternative, just keep the default branch name as master in your remote repository.

Nicholas
  • 2,800
  • 1
  • 14
  • 21
  • Thanks! How do I change the branch to main in the existing repo and set it as the default remote? – electrophile Mar 14 '21 at 07:33
  • 1
    If it is an existing remote, then it is pretty simple. Just do `git branch main`, `git checkout main`, `git push -u origin main`, then set that `main` branch as the default branch. You can find it in GitHub repository settings (in one of the top tabs). After you have successfully updated the default branch to `main`, feel free to delete the `master` branch. – Nicholas Mar 14 '21 at 07:38
2

With the recent change of master to main in Github, it seems I need to first push to master and then merge with main.

Actually no, not with Git 2.31 (which will be released next Monday) * Any git clone of an empty GitHub repository will create a local repository with 'main' as a default branch, even if init.defaultBranch was still set on master!

With protocol v2, GitHub will communicate to the client the name of the remote default branch (main for GitHub) to the local Git repository created by git clone.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250