Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

Github problem: Git pushes to master branch, not main and can't merge the two

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, 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. Also tried to run git push -u origin main, the error given:

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 aswell, with the same errors as a result.

What can I do to solve this?

question from:https://stackoverflow.com/questions/65924087/github-problem-git-pushes-to-master-branch-not-main-and-cant-merge-the-two

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...