简体   繁体   English

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

[英]Issue pushing to a GitHub repository created with “Initialize repo with a README” option

I tried to create a GitHub repository using the instructions in the GitHub documentation , except instead of making a README locally, I initialized my GitHub repository with the README option. 我尝试使用GitHub文档中的说明创建一个GitHub存储库,除了README本地创建README之外,我使用README选项初始化了我的GitHub存储库。 After trying to push, though, I get this error I don't fully understand: 但是,在尝试推送之后,我得到了这个错误,我不完全理解:

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.

I couldn't pull the repository either, so I tried manually downloading, adding, and committing the README file I had created virtually and tried to push again, and got a new error: 我也无法拉出存储库,所以我尝试手动下载,添加和提交我虚拟创建的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.

This leaves me with four questions: 这让我有四个问题:

1.. Why does it not work to initialize a remote repository on GitHub with a README , and then try to connect the GitHub repository with a previously existing existing local repository? 1 ..为什么使用README在GitHub上初始化远程存储库不起作用,然后尝试将GitHub存储库与以前存在的现有本地存储库连接?

  1. Why could I not pull when I tried to fix this error? 当我尝试修复此错误时,为什么我不能拉?

  2. Why could I still not push and initialize the connection to my GitHub remote after I added the README from GitHub to my local repository manually? 为什么在将GMEHub中的README手动添加到本地存储库后,我仍然无法推送和初始化与GitHub远程的连接?

  3. How do I create a GitHub repository with an initialized README and connect it to a local repo without causing these errors? 如何使用初始化的README创建GitHub存储库并将其连接到本地存储库而不会导致这些错误?

The errors are saying that your repo has changes that you don't have yet, because you added the README to the remote one when you set it up. 错误是说您的仓库还有您尚未进行的更改,因为您在设置时将README添加到远程README If you've already got local changes or a local repo, you need to initialise an empty repository on Github, and then you can push. 如果您已经有本地更改或本地存储库,则需要在Github上初始化一个空存储库,然后您可以推送。 You'll have to add the remote though, something like git remote add https://github.com/username/repo.git . 你必须添加遥控器,比如git remote add https://github.com/username/repo.git

Downloading the README manually and adding and committing will probably produce a different commit ID, and put it at a different point in the commit history, which is why it's not detected as the same one. 手动下载README并添加和提交可能会产生不同的提交ID,并将其放在提交历史记录中的不同位置,这就是为什么它不会被检测为相同的提交ID。

1) Why does it not work to initialize a remote repository on github with a README, and then try to connect the github repo with a previously existing existing local repo? 1)为什么用README初始化github上的远程存储库是不行的,然后尝试将github repo与以前存在的现有本地存储库连接?

When Github adds the README it commits it, and then this is the first commit. 当Github添加自述文件时,它会提交它,然后这是第一次提交。 If you have a local repo, the first commit locally will be different, so they won't match up. 如果您有本地存储库,则本地第一次提交将不同,因此它们将不匹配。

2) Why could I not pull when I tried to fix this error? 2)当我试图修复此错误时,为什么我不能拉?

Probably because of the above, or the remote reference didn't add in properly, depending on how you added it. 可能是因为上面的原因,或remote引用未正确添加,具体取决于您添加它的方式。

Generally, if you're creating locally first, you would go: 一般来说,如果你先在本地创建,你会去:

# 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 

Or if it already exists on Github or a different remote server: 或者,如果它已存在于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) Why could I still not push and initialize the connection to my github remote after I added the README from github to my local repository manually? 3)为什么我在将github的README手动添加到本地存储库后,仍然无法推送和初始化与github远程的连接?

That's not how the changes work with Git; 这不是改变如何与Git一起工作; they're a big list of sequential commits in a chain. 它们是链中顺序提交的重要列表。 Each commit has one or two parent commits, and the commit IDs aren't sequential either. 每个提交都有一个或两个父提交,提交ID也不是顺序的。

See the Git website for some diagrams on the branching and commits here: http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is 有关分支和提交的一些图表,请访问Git网站: http//git-scm.com/book/en/Git-Branching-What-a-Branch-Is

4) How do I create a github repo with an initialized README and connect it to a local repo WITHOUT causing the errors below? 4)如何使用初始化的README创建github仓库并将其连接到本地仓库而不会导致下面的错误?

If you have an existing local repo you shouldn't create one with the initialised README. 如果您有一个现有的本地仓库,则不应使用初始化的README创建一个。 If it's blank on Github when you create it, you can push up your existing repository with no errors. 如果在创建它时它在Github上是空白的,那么您可以毫无错误地推高现有存储库。 If it has the README, you have to git clone the Github repo, and then add your changes to that folder, commit the changes, and then push. 如果它具有自述文件,则必须git clone Github存储库,然后将更改添加到该文件夹​​,提交更改,然后按下。 Adding the README is for when you have a new project and you're creating the Github repo first, and then you clone the project and start working in that location. 添加README是为了在您有一个新项目并且首先创建Github存储库时,然后克隆该项目并开始在该位置工作。 If you have an existing repository locally don't use that option. 如果您在本地具有现有存储库,请不要使用该选项。

Let's say you have a local repository: 假设您有一个本地存储库:

$ git log --oneline
8e7e8d4 hello.txt

The local repository has a single file: 本地存储库只有一个文件:

$ ls
hello.txt

Now you create a new repository via the GitHub web interface and initialize it with a README file. 现在,您通过GitHub Web界面创建一个新的存储库,并使用README文件对其进行初始化。 At this point, your two repositories have divergent histories. 此时,您的两个存储库具有不同的历史记录。 You can add the remote repository to your local repository: 您可以将远程存储库添加到本地存储库:

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

But attempting to pull from this repository will yield an error: 但是尝试从此存储库中提取将产生错误:

$ 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

The important part of this error message is: 此错误消息的重要部分是:

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

This is git telling you that while your current branch in your local repository is not associated with any branch in the remote repository, so it doesn't know what to do. 这是git告诉您,虽然您本地存储库中的当前分支与远程存储库中的任何分支都没有关联,但它不知道该怎么做。 You can provide it with a remote branch name explicitly: 您可以明确地为其提供远程分支名称:

$ git pull origin master

This will create a merge commit (and will probably prompt you for a commit message). 这将创建一个合并提交(并可能会提示您提交提交消息)。 Once the commit is complete, you can see that the local history now contains both our local commit as well as the commits from the GitHub repository: 提交完成后,您可以看到本地历史记录现在既包含本地提交,也包含GitHub存储库的提交:

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

And our working directory now has a merged set of files: 我们的工作目录现在有一组合并的文件:

$ ls
hello.txt  README.md

And we can push this to our remote repository. 我们可以将其推送到我们的远程存储库。 Just typing git push will result in an error similar to what we saw earlier: 只需输入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

So: 所以:

$ 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.

And now we're all in sync. 现在我们都在同步。

Update : Regarding your questions about --set-upstream : 更新 :关于--set-upstream

When you checkout a local branch that matches the name of a remote branch, git will set up the upstream association for you automatically. 当您签出与远程分支的名称匹配的本地分支时, git将自动为您设置上游关联。 For example, if the remote repository in this example also had a branch "development", and I did this after adding the remote: 例如,如果此示例中的远程存储库也有一个分支“development”,我添加远程后执行了此操作:

$ git checkout development

I would see: 我会看见:

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

On the other hand, if you already have a branch checked out when you add the remote, as in the first part of this example, you need to use --set-upstream to tell git that you want your local branch to track the remote branch. 另一方面,如果您在添加遥控器时已经检出分支,如本示例的第一部分所示,您需要使用--set-upstream告诉git您希望本地分支跟踪远程科。

As an aside, note that there's no requirement that your local branch match the name of a remote branch. 另外,请注意,不要求本地分支与远程分支的名称匹配。 You are free to do something like: 您可以自由地执行以下操作:

git checkout master
git push --set-upstream origin patches

So that from this point on, git push on your master branch will push to the remote patches branch. 因此,从这一点开始, master分支上的git push将推送到远程patches分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM