问题推送到使用“使用README初始化repo”选项创建的GitHub存储库

时间:2014-04-25 23:28:08

标签: git github

我尝试使用the instructions in the GitHub documentation创建一个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 ..为什么用README在GitHub上初始化远程存储库不起作用,然后尝试将GitHub存储库与以前存在的现有本地存储库连接?

  1. 当我尝试修复此错误时,为什么我不能拉?

  2. 为什么在手动将GitHub中的README添加到本地存储库后,我仍然无法推送和初始化与GitHub遥控器的连接?

  3. 如何使用已初始化的README创建GitHub存储库并将其连接到本地存储而不会导致这些错误?

2 个答案:

答案 0 :(得分:11)

错误表明您的仓库还有您尚未进行的更改,因为您在设置时将README添加到远程仓库。如果您已经有本地更改或本地存储库,则需要在Github上初始化一个空存储库,然后您可以推送。您必须添加遥控器,例如git remote add https://github.com/username/repo.git

手动下载README并添加和提交可能会产生不同的提交ID,并将其放在提交历史记录的不同位置,这就是为什么它不会被检测为同一个。

  

1)为什么用README初始化github上的远程存储库是不行的,然后尝试将github repo与以前存在的现有本地存储库连接?

当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上是空白的,那么您可以毫无错误地推高现有存储库。如果它具有自述文件,则必须git clone Github存储库,然后将更改添加到该文件夹​​,提交更改,然后按下。添加README是为了在您有一个新项目并且首先创建Github存储库时,然后克隆该项目并开始在该位置工作。如果您在本地拥有现有存储库,请不要使用该选项。

答案 1 :(得分:8)

假设您有一个本地存储库:

$ git log --oneline
8e7e8d4 hello.txt

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

$ ls
hello.txt

现在,您可以通过GitHub Web界面创建一个新的存储库,并使用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

因此,从此时起,git push分支上的master将推送到远程patches分支。