简体   繁体   中英

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM