使用“初始化存储库自述文件”选项创建的 GitHub 存储库推送问题

15

我尝试使用GitHub文档中的说明创建GitHub存储库,但与其在本地创建README,我使用README选项初始化了我的GitHub存储库。在尝试推送后,我遇到了这个我不完全理解的错误:

kirby:cs61as_SCIP_schython \**user**$ git push origin master
https://github.com/chris-marie/cs61as_SICP_schython.git 
! [rejected] 
master -> master (fetch first) error: failed to push some refs to
'https://github.com/chris-marie/cs61as_SICP_schython.git' hint:
Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository
pushing hint: to the same ref. You may want to first merge the remote
changes (e.g., hint: 'git pull') before pushing again. hint: See the
'Note about fast-forwards' in 'git push --help' for details.

我也无法拉取存储库,于是我尝试手动下载、添加和提交我曾经虚拟创建的README文件,然后再次尝试推送,结果出现了一个新错误:

kirby:cs61as_SCIP_schython \**user**$ git push origin master
https://github.com/chris-marie/cs61as_SICP_schython.git 
! [rejected] 
master -> master (non-fast-forward) error: failed to push some refs to
'https://github.com/chris-marie/cs61as_SICP_schython.git' hint:
Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git
pull') hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这让我有了四个问题:

1. 为什么在GitHub上用一个README初始化远程仓库,然后尝试将GitHub仓库与之前已有的本地仓库连接,不能成功?

  1. 当我尝试修复此错误时,为什么无法拉取(pull)代码?

  2. 即使我手动从GitHub将README添加到我的本地仓库后,为什么仍然无法推送(push)并初始化与GitHub远程仓库的连接?

  3. 如何创建一个带有已初始化的README的GitHub仓库,并将其连接到本地repo而不导致这些错误?


当你说你无法“pull”仓库时,它给出了什么错误? - Leigh
它说:“致命错误:无效的refspec'https://github.com/chris-marie/cs61as_SICP_schython.git'”。 - Chris Marie
你在设置时,最初是使用 git clone 还是 git init 然后再使用 git remote add ... - Leigh
在我的本地目录中,我想要创建一个仓库,所以我使用了 git init 命令。在尝试将本地仓库连接到远程仓库之前,我还正确地添加了文件并进行了第一次提交。 - Chris Marie
这让我有四个问题。请每个问题分别回答。 - Lightness Races in Orbit
你可以使用命令"git pull origin master --allow-unrelated-histories",然后你会看到魔法:D - AITAALI_ABDERRAHMANE
2个回答

22

错误提示是说你的存储库有变更,而你还没有这些变更,因为在设置时添加了README到远程存储库中。如果您已经进行了本地更改或拥有本地存储库,则需要在Github上初始化一个空存储库,然后才能推送。不过您还需要添加远程引用,例如git remote add https://github.com/username/repo.git

手动下载README并添加和提交可能会产生不同的提交ID,并将其放置在提交历史的不同位置,因此它不会被检测为相同的提交。

1)为什么使用README初始化远程存储库,然后尝试将github存储库连接到先前存在的本地存储库不起作用?

Github添加README后会进行提交,然后这是第一次提交。如果您有一个本地存储库,则本地的第一次提交将是不同的,因此它们不会匹配。

2)为什么我尝试修复此错误时无法拉取?

可能是由于上述原因或者remote引用未成功添加,具体取决于您如何添加它。

通常,如果您首先创建本地存储库,则会进行以下操作:

# Set up the Git repo locally, with no commits in it.
git init   
# Add a new file.
git add file1
# Commit the change.
git commit
# Tell Git where your remote is.
git remote add origin https://github.com/user/repo.git 
# Push the default 'master' branch to the above remote called 'origin'.
git push origin master 
如果它已经存在于Github或其他远程服务器上:
# Download the existing repo, with all of the history.
git clone https://bitbucket.org/user/repo.git
# Add a new file or modified file.
git add file1
# Commit the change.
git commit
# Push to the remote that you downloaded from with clone on branch master.
git push origin master

3) 如果我将github上的README文件手动添加到本地仓库中,为什么我仍然无法推送和初始化与github远程的连接?

这不是Git的工作方式。它们是一系列顺序提交的大型列表。每个提交都有一个或两个父提交,提交ID也不是连续的。

请参阅Git网站上关于分支和提交的一些图表:http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is

4) 如何创建一个带有初始化README并将其连接到本地仓库的Github存储库,而不会引起以下错误?

如果您已经有一个现有的本地仓库,则不应使用初始化README创建一个新的。 如果在Github上创建时为空白的,则可以将现有存储库上传而不出错。 如果它有README,则必须git cloneGithub存储库,然后将更改添加到该文件夹中,提交更改,然后推送。 添加README是为了当您有一个新项目并首先创建Github存储库时,您可以克隆项目并开始在该位置工作。 如果您已经具有本地存储库,请勿使用该选项。


我明白了,要么这个,要么那个 - 不能两者都有。谢谢!你提供的网站也非常有帮助,然而,既然我的第一次尝试产生了这个分支问题,为什么我不能像解决其他分支问题一样用git merge来解决它呢? - Chris Marie
在普通的分支问题中,你们共享一个共同的历史,而这个问题是它们本质上是完全不同的存储库,具有不同的初始节点。 - Leigh
我在创建了一个空的代码库后,将本地文件推送到了Github上,并在Github上创建了一个readme文件。现在该怎么办?!@Leigh - Akhila

11

假设您有一个本地仓库:

$ git log --oneline
8e7e8d4 hello.txt

本地存储库只有一个文件:

$ ls
hello.txt

现在您可以通过GitHub网页界面创建一个新的存储库,并使用README文件初始化它。此时,您的两个存储库具有不同的历史记录。您可以将远程存储库添加到本地存储库:

$ git remote add origin git@github.com:larsks/samplerepo.git

但是尝试从这个仓库拉取代码将会产生一个错误:

$ git pull
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:larsks/samplerepo
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

这个错误信息中重要的部分是:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

这是git告诉你,虽然你的本地仓库当前分支与远程仓库没有关联,因此不知道该怎么做。你可以显式地提供一个远程分支名称:

$ git pull origin master

这将创建一个合并提交(可能会提示您输入提交信息)。完成提交后,您可以看到本地历史记录现在包含了我们的本地提交和GitHub存储库中的提交:

$ git log --oneline
7f1231a Merge branch 'master' of github.com:larsks/samplerepo
5f0d62e Initial commit
8e7e8d4 hello.txt

现在,我们的工作目录中有一组已合并的文件:

$ ls
hello.txt  README.md

我们可以将这个推送到远程仓库。只需输入git push,就会出现与之前看到的类似的错误:

$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

所以:

$ git push --set-upstream origin master
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 543 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To git@github.com:larsks/samplerepo.git
   5f0d62e..7f1231a  master -> master
Branch master set up to track remote branch master from origin.

现在我们都同步了。

更新: 关于--set-upstream的问题:

当您检出与远程分支名称匹配的本地分支时,git将自动为您设置上游关联。例如,在添加远程之后,如果远程存储库中也有一个名为"development"的分支,并且我执行了以下操作:

$ git checkout development

我希望看到:

Branch development set up to track remote branch development from origin.
Switched to a new branch 'development'

另一方面,如果您在添加远程分支时已经有一个分支被检出,就像此示例的第一部分中一样,您需要使用--set-upstream告诉git您想要本地分支跟踪远程分支。

顺带提一下,注意您的本地分支不必与远程分支的名称匹配。您可以自由地执行类似以下操作:

git checkout master
git push --set-upstream origin patches
因此,从现在开始,在您的master分支上运行git push命令将推送到远程patches分支。

啊,看起来Leigh也在同时回答。干杯! - larsks
谢谢!不过,我有一个问题,我和这个人面临相同的困境吗:https://dev59.com/dm025IYBdhLWcg3wT0Hq ,也就是说,我应该记得每次声明还是设置默认值? - Chris Marie
另外……如果可以问的话……我有一个不在GitHub上的个人服务器上的不同项目git存储库,我不需要使用--set-upstream,并且我不认为我曾经使用git push -u origin my_branch声明过默认值,这是回答这个问题的人建议的。为什么我需要在一种情况下使用--set-upstream而在另一种情况下不需要呢? - Chris Marie
我已经在这里更新了答案,请告诉我如果这不回答你的问题。 - larsks
3
@larsks,我已经为你的回答点赞,但是在你的流程中有些情况会出现这样的信息:fatal: refusing to merge unrelated histories,此时开发者必须执行 git pull origin master --allow-unrelated-histories - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接