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:
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.
git branch -M main
, which is what they do tell you to do. – Mcmillen