GitHub problem: Git pushes to master branch, not main and the two can't be merged
Asked Answered
S

2

14

Every time I create a repository on GitHub and push my files it creates two branches, the main branch and the master branch. All the changes go to the master branch and when I go to 'Compare and pull request' it says there's nothing to compare, so there's no way to push to the main branch.

These are the steps I took: Go to GitHub, create a repository. Go to my folder and run git init,git add ., git commit -m "first commit", git remote add origin my@repository, and git push -u origin master Now I know that I need to create a main branch, so I also run git checkout -b main, which runs successfully, but then, when I run git push --set-upstream origin main, it throws an error:

 ! [rejected]          main -> main (non-fast-forward)
error: failed to push some refs to '[email protected]:myuser/myrepo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I also run git pull but it says it's already up to date. Also on main branch I run git merge master, but it gives the message: Already up to date, and vice versa, from master branch tried to merge the main one (just to check) and the same message is given. I've also tried runing git push -u origin main, which gives the following error:

error: src refspec main does not match any
error: failed to push some refs to '[email protected]:myuser/myrepo.git'

Note that I also tried all those commands in different order as well, with the same errors as a result.

What can I do to solve this?

Sardella answered 27/1, 2021 at 17:16 Comment(7)
are you working with forks?Dalpe
Unclear. Did you want the default branch to be master or main? If main, why do you not follow GitHub instructions? If master, why create main?Mcmillen
“Now I know, I need to create a main branch” No you don’t. Why don’t you just stop after the initial push?Mcmillen
@Mcmillen there's no main branch in the terminal through the whole process, only master is created, but on github there's only main, so yes.Sardella
@Mcmillen On github they changed it so the main branch is now called main. I want to push to the default branch on github, which is main. Followed github instructions, as explained above, but it leads to this situationSardella
@x-rw No, just from local repositorySardella
But you didn't follow the GitHub instructions. You pushed master, which is not what they tell you to do, and you didn't git branch -M main, which is what they do tell you to do.Mcmillen
M
31

The whole question is based on a misconception, which is summarized in your first sentence:

Every time I create a repository on github and push my files it creates two branches, the main branch and the master branch

No. That is not something "it" does. That is something you are doing. And if you don't want that to happen, then don't do it. To avoid doing it, you need to think first, before acting. You need to decide how you want things to be, and only then you take action to make it so.

Let's talk about the decisions you need to make, and how to make them come true.


First of all, when you create the repo at GitHub, you need to decide whether it is to be completely empty or whether it should have any initial elements such as a README.

I like my repo to be completely empty. In that case it has no branches at all. Everything is thus up to you at the local end. GitHub provides perfectly clear instructions as to what to do at this point. Let's say you have an existing local repo, as you have created:

git init
git add .
git commit -m "first commit"

Now your default branch is master. So you have to decide now whether to keep that or change it to main. Either way, do not push until you have made a decision!

If you decide to keep it as master, then just add the remote and push, exactly as you did:

git remote add origin my@repository
git push -u origin master

Now stop. You say:

I need to create a main branch

No, you don't! You have made master your primary branch, so just keep using it as the primary branch.

On the other hand if you decide you want main as your primary branch, then follow the instructions that GitHub gives you:

git remote add origin my@repository
git branch -M main
git push -u origin main

And again, stop.

Either way, you have pushed just one initial branch, either master or main. But do not do what you are doing, namely, push master and then mysteriously change your mind and decide you wish it had been main. That is how you've gotten yourself into an unnecessary tangle.


For the sake of completeness, let's say you initially decide when you create the GitHub repo that you want it to have initial contents. That is a bad decision if you are going to be starting with git init at the local end, and GitHub tells you so in no uncertain terms. But let's proceed with this alternate scenario anyway.

Suppose you elect to start your GitHub repo with a Readme. Okay, so now the GitHub repo does have an initial branch, and it is main. So now once again you have two choices about what to do on your local machine: you must decide whether to start locally with an empty copy of this same GitHub repo, or whether to start locally with a repo that you may already have.

If you decide to start empty locally, then do not say git init. Say git clone and clone the remote GitHub repo to your local machine. This is far and away the best choice in this situation.

But let's say you don't do that. Let's say you decide create a separate local repo from scratch. For instance, you do this:

 $ git init what; cd $_
 $ echo howdy > testing.txt; git add .; git commit -minitial
 $ git remote add origin my@repository

Now you are in a serious mess, because:

  • Your remote repo and your local repo both have stuff that the other doesn't have.

  • Your local repo has master but your remote repo has main.

How are you going to push this? Well, I'm going to assume that you accept the notion, imposed on you now by GitHub, that you should use main as your main branch. So here is what you do:

$ git branch -M main
$ git pull origin main --allow-unrelated
$ git push -u origin main

Now you have folded your remote repo and your local repo together with the same branch and the same contents.

Mcmillen answered 27/1, 2021 at 19:15 Comment(4)
In gitlab, even if I unselect the "create readme" to open an empty branchless project, the branch "main" is automatically created. any idea?Gone
The main branch is created in theory as the remote head, but that doesn't matter unless commits are added to it. Without commits, it is an "unborn branch". See my biteinteractive.com/of-git-and-github-master-and-main — it's about GitHub but the key points are the same.Mcmillen
This doesn't seem to reflect the local reality, here. Today, I did git init, add, commit and push to master after creating a new gh repo w/o any initial remote content. Nevertheless, I ended up with a main (default) and a (pushed) master branch. You may hint at what I may be missing or not.Tatty
Correction to the above comment: this might be caused by selecting a license when creating the repo, resulting in a LICENSE file.Tatty
D
-1

Apply these commands:

git rebase master

git add theFiles

git commit -m "your commit"

git push -f
Dalpe answered 27/1, 2021 at 19:4 Comment(1)
Can you add some explanation about what this does?Reformed

© 2022 - 2024 — McMap. All rights reserved.