Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github directly push to main

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?

like image 411
electrophile Avatar asked Jan 24 '23 10:01

electrophile


2 Answers

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.

like image 199
Nicholas Avatar answered Jan 27 '23 07:01

Nicholas


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.

like image 27
VonC Avatar answered Jan 27 '23 05:01

VonC