Can't push to main on new git repo

Viewed 19102

I am realtivly new to git and have created a new github repo after the update which switched master to main.

The first problem I had was that when I pulled git pull origin main nothing came down. At the time the repo continued a .gitignore, readme, and a licence. I solved this by manually downloading them.

However, I am unable to push to main. When I tried to run git push origin main I got an error message and was told by git to run the following: git push --set-upstream origin master. After running that, I can now push to master but not main. When I try to run git push origin main I get the following error:

error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/Ebony-Ayers/CS-style-text-format'

How can I make it so that I can push to main rather than master as according to the repo on the github website main is my default branch? Any help would be greatly appreciated.

2 Answers

What is your local branch called? git push origin main tries to take your local branch called main and pushes it to origin. If you do not have a local branch called main, then git won't be able to push anything, which seems to be why you are receiving this error. What you can do is run the following:

git checkout -b main
git push --set-upstream origin main

and this should correct the issue.

To break down what's happening:

git checkout -b main

This command creates a new local branch called main. It is identical to the branch that you are working off of (I'm assuming master). You can check what branch you are on either by running git status and in the several lines that are printed the branch name should show, or more clearly git branch which will list all local branches but highlight the current branch you are on. When creating a new branch, any local changes (whether staged or unstaged) are also maintained and not removed from the working directory.

git push --set-upstream origin main

This command pushes to origin, which is the name of the remote repository on GitHub, and pushes your local branch main, which we created above. This will create a new branch in GitHub with the same name as well, and the --set-upstream flag tells git that origin main is the default remote/branch combo that you want to push to, and you can simply run git push and it will by default push any local changes on main

Once the new main branch is pushed, and you confirm that everything is there as expected, you should be able to delete the master branch directly from GitHub. You can also delete the branch locally by running:

git branch -D master

Make sure not to delete either the local or the remote master branch until you have confirmed that everything you need is in the new main branch. This should not be the case, but it never hurts to double check.

When you want to push your local repo to remote in GitHub but your local repo branch is named 'master' and your remote is named 'main' you wont be able to push.

The solution as described by GitHub documentation is to rename your local repo to 'main'.

To do this your current branch needs to 'master' Verify your branch using command :

git branch

this will list all branches and highlight your current branch.

Then to rename the branch the command is :

git branch -M main

The flag -M is a shortcut for flags : --move --force. The move means move/rename a branch, together with its config and reflog.

Additionally, as commented by (#Sal from Dec 24, 2020 at 8:20) for convenience you can set 'main' as the default branch with:

 git push --set-upstream origin main

Once 'main' is default then you can just do git push or git pull without adding the branch name to the command.

Related