简体   繁体   English

GIT:如何将第二个本地存储库与现有的GitHub存储库连接?

[英]GIT: how to connect a second local repository with an existing GitHub repository?

I am learning to handle the GIT repositories. 我正在学习处理GIT存储库。 I have a repository on GitHub and I have cloned it locally. 我在GitHub上有一个存储库,并且已在本地克隆它。 I have modified some files, then add -A, commit -m and git push origin master. 我已经修改了一些文件,然后添加-A,commit -m和git push origin master。 So far so good. 到现在为止还挺好。

Now, I have created another local repository with Git GUI, I have placed the files of the site-web (django 2.0), and again add, commit, after: 现在,我使用Git GUI创建了另一个本地存储库,放置了站点Web(django 2.0)的文件,然后在以下位置再次添加,提交:

$ git remote add origin https://github.com/Frank-Mascarell/LibrosWeb.git
$ git remote -v
origin  https://github.com/Frank-Mascarell/LibrosWeb.git (fetch)
origin  https://github.com/Frank-Mascarell/LibrosWeb.git (push)
$ git status
On branch master
nothing to commit, working tree clean
$ git fetch origin


$ git push origin master
To https://github.com/Frank-Mascarell/LibrosWeb.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/Frank-
Mascarell/LibrosWeb.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.

and I have also tried: 我也尝试过:

$ git pull origin master
From https://github.com/Frank-Mascarell/LibrosWeb
* branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Why are you giving these errors? 为什么要给出这些错误? It seems that the two repositories are not well connected, and I can not upload or download. 看来这两个存储库没有很好地连接,并且我无法上传或下载。

If I understand correctly, you have created a new repository from scratch, and you have added a remote to that. 如果我理解正确,那么您从头开始创建了一个新的存储库,并为此添加了一个远程存储库。

In the first scenario, you are trying to push changes to a branch which does not share the same history as your branch. 在第一种情况下,您尝试将更改推送到与分支没有相同历史记录的分支。

In the second scenario, you are trying to merge changes from the remote branch into your branch. 在第二种情况下,您尝试将更改从远程分支合并到分支中。 This fails because the two branches do not share any common history. 之所以失败,是因为两个分支没有共享任何共同的历史记录。

If you instead start over and instead of initialising a new repository, make sure you clone the original one and start from there. 如果您是重新开始而不是初始化新的存储库,请确保克隆原始存储库并从那里开始。 This will allow you to both push new changes, and to merge or rebase on top of existing commits. 这将使您既可以推送新更改,又可以在现有提交的基础上进行合并或重新设置基础。

The error message gives a clue as to the issue. 该错误消息提供了有关该问题的线索。 The two repositories have unrelated histories. 这两个存储库具有不相关的历史记录。

In git, you aren't solely dealing with files and changes to files, though that is what you are getting out of it. 在git中,您将不仅仅处理文件和文件更改,尽管这是您要摆脱的。 Each commit in git contains a reference to every commit that lead up to it, giving you a full and complete history of how things got to be the way they are. git中的每个提交都包含对导致该提交的每个提交的引用,从而为您提供了有关事情如何发展的完整完整的历史记录。

What happened is you have three repositories, two are linked, and one was created brand new but given identical files. 发生的情况是您有三个存储库,两个存储库已链接,并且一个存储库是全新创建的,但具有相同的文件。

So your repository started on github, and then was cloned down locally, and you can push and pull back and forth, mirroring not just the data but the entire commit history. 因此,您的存储库始于github,然后在本地克隆,您可以前后推拉,不仅镜像数据,还镜像整个提交历史记录。 They both have a complete reference of your project from start to finish. 他们从头到尾都对您的项目有完整的参考。

The new repository just has a snapshot of the most recent state of the project, without any history. 新的存储库仅具有项目最新状态的快照,没有任何历史记录。

It's possible to modify your new local repository to get it into a state where it can push to an existing branch again, but it's probably more complicated than necessary. 可以修改新的本地存储库,使其进入可以再次推送到现有分支的状态,但这可能比必要的更为复杂。

The easiest thing to do is just to clone the new repository again, either from github or from your first local repository. 最简单的方法是从github或第一个本地存储库中再次克隆新存储库。

However, if you have changes you've already made to the second local repository, you'll want to copy those changes into one of your cloned repositories, make new commit(s) and push from there. 但是,如果您已经对第二个本地存储库进行了更改,则需要将这些更改复制到一个克隆存储库中,进行新的提交并从那里推送。

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

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