Github问题:Git推送到master分支,而不是main,并且不能合并两者 [英] Github problem: Git pushes to master branch, not main and can't merge the two

查看:273
本文介绍了Github问题:Git推送到master分支,而不是main,并且不能合并两者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我在github上创建存储库并推送文件时,它都会创建两个分支,即main分支和master分支.所有更改都转到master分支,当我转到比较并提取请求"时,它说没有什么可比较的,因此没有办法推送到main分支.

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.

这些是我采取的步骤:转到github,创建一个存储库.转到我的文件夹并运行 git init git add. git commit -m首次提交" git remote addorigin my @ repository git push -u origin master 现在我知道,我需要创建一个main分支,所以我还运行了成功运行的 git checkout -b main ,但是当我运行 git push --set-upstream origin main 会引发错误:

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 'git@github.com: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.

我还运行了 git pull ,但它说它已经是最新的了.同样在主分支上,我运行 git merge master ,但是它给出了消息:已经更新,反之亦然,来自master分支的试图合并主分支(只是检查),并给出相同的消息.还尝试运行 git push -u origin main ,给出错误:

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 'git@github.com:myuser/myrepo.git'

请注意,我还以不同的顺序尝试了所有这些命令,结果出现了相同的错误.

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

该怎么办?

推荐答案

整个问题是基于一个误解,在您的第一句话中对此进行了总结:

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

每次我在github上创建存储库并推送文件时,它都会创建两个分支,即main分支和master分支

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.

首先,当您在GitHub上创建存储库时,您需要确定是完全为空,还是应该具有诸如README之类的任何初始元素.

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.

我希望我的仓库完全为空.在这种情况下,它根本没有分支.因此,一切都取决于本地端.GitHub提供了有关此时该怎么做的非常清晰的说明.假设您已经创建了一个现有的本地仓库:

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"

现在您的默认分支为 master .因此,您现在必须决定是保留它还是将其更改为 main .无论哪种方式,在您做出决定之前,都不要按下

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!

如果您决定将其保留为 master ,则只需添加遥控器并进行推送即可,就像您所做的一样:

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

现在停止.你说:

我需要创建一个主分支

I need to create a main branch

不,你不知道!您已经将 master 设置为主要分支,因此只需继续将其用作主要分支即可.

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

另一方面,如果您决定您想要 main 作为您的主要分支,那么请遵循GitHub给您的指示:

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

再次,停止.

无论哪种方式,您只推送了一个初始分支, master main .但是,您在做什么,即按 master ,然后神秘地改变主意,并决定您希望它是主要.这就是您陷入不必要的纠结的方式.

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.

为了完整起见,假设您最初在创建GitHub存储库时决定您希望它具有初始内容.如果要从本地开始使用 git init ,这是一个错误的决定,并且GitHub毫无疑问地告诉您.但是无论如何,让我们继续这个替代方案.

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.

假设您选择使用自述文件启动GitHub存储库.好的,所以 now GitHub存储库 有一个初始分支,并且它是 main .因此,关于本地计算机上的操作,现在再次有两个选择:您必须决定:是否从该同一个GitHub存储库的空副本开始本地存储,或者是否从具有该GitHub存储库的空副本开始本地存储.你可能已经有.

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.

如果您决定在本地开始为空,则 git init .说 git clone 并将远程GitHub存储库克隆到您的本地计算机.在这种情况下,这无疑是最好的选择.

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.

您的本地存储库具有 master ,但您的远程存储库具有 main .

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

您打算如何推动?好吧,我假设您接受GitHub现在强加给您的概念,即您应该使用 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.

这篇关于Github问题:Git推送到master分支,而不是main,并且不能合并两者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆