项目可以有多个来源吗?

时间:2012-07-27 15:14:01

标签: git github repository

项目是否有两个(或更多)"起源"在Git?

我想将一个项目推送到githubHeroku服务器。

具体来说,添加github存储库时会出现此错误:

$ git remote add origin https://github.com/Company_Name/repository_name.git
fatal: remote origin already exists.

9 个答案:

答案 0 :(得分:208)

您可以拥有任意数量的遥控器,但您只能拥有一个名为“origin”的遥控器。称为“origin”的远程控件并不特殊,只不过它是克隆现有存储库时由Git创建的默认远程控制器。您可以配置第二个遥控器,从该遥控器推/拉,并设置一些分支来跟踪该遥控器而不是原点的分支。

尝试添加名为“github”的遥控器:

$ git remote add github https://github.com/Company_Name/repository_name.git

# push master to github
$ git push github master

# Push my-branch to github and set it to track github/my-branch
$ git push -u github my-branch

# Make some existing branch track github instead of origin
$ git branch --set-upstream other-branch github/other-branch

答案 1 :(得分:74)

作为后来绊倒这个问题的人的旁注,有可能一次将原始数据推送到多个git存储库服务器。

您可以使用以下命令将另一个URL添加到原始远程来实现此目的。

git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git

答案 2 :(得分:22)

这是一个带有多个遥控器的示例项目,GitHub& GitLab:

  1. 为GitHub添加远程仓库

    $ git remote add github https://github.com/Company_Name/repository_name.git
    
  2. 为GitLab添加远程仓库

    $ git remote add gitlab https://gitlab.com/Company_Name/repository_name.git
    
  3. 现在项目中有多个遥控器。仔细检查git remote -v

    $ git remote -v
    github https://github.com/Company_Name/repository_name.git (fetch)
    github https://github.com/Company_Name/repository_name.git (push)
    gitlab https://gitlab.com/Company_Name/repository_name.git (fetch)
    gitlab https://gitlab.com/Company_Name/repository_name.git (push)
    
  4. 如何推送到多个存储库?

    $ git push github && git push gitlab
    

答案 3 :(得分:4)

error[E0433]: failed to resolve: use of undeclared type or module `module_b`
 --> src/lib.rs:3:9
  |
3 |         module_b::function2(a)
  |         ^^^^^^^^ use of undeclared type or module `module_b`

现在您有2个起源。

答案 4 :(得分:1)

您可以通过提供其他名称而不是来源来将另一个远程帐户添加到您的存储库。您可以使用诸如origin2之类的名称。 因此您的git命令可以修改为

git remote add origin2 https://github.com/Company_Name/repository_name.git

答案 5 :(得分:1)

git remote add origin2 https://github.com/Company_Name/repository_name.git

并用于推送:

git push -u origin2 master

答案 6 :(得分:1)

本地存储库可以链接到多个远程存储库

但是,这些链接中只有一个可以称为origin。其余链接需要具有不同的名称

因此,为了正确回答这个问题,我们需要了解起源是什么。

让我举例说明。

假设您有一个名为remote repository的{​​{1}},然后将该远程存储库克隆到了本地计算机上,从而有了amazing-project。然后,您将获得类似下图所示的内容:

enter image description here

因为您克隆了存储库。远程存储库和本地存储库已链接

如果运行命令local repository,它将列出所有链接到本地​​存储库的远程存储库。在那里,您将看到为了从远程存储库中推送或获取代码,您将使用 shortname 'origin'。 enter image description here

现在,这可能有点令人困惑,因为在GitHub(或远程服务器)中,该项目称为“ amazing-project”。那么为什么看起来远程存储库有两个名称呢?

enter image description here

存储库的名称之一就是它在GitHub或某个地方的远程服务器上的名称。可能会像项目名称这样。在我们的例子中,这就是“惊人的项目”。

存储库的另一个名称是 shortname ,它在我们本地存储库中具有与存储库URL相关的名称。这是我们要从该远程存储库中推送或获取代码时将要使用的简称。而且这种短名称的行为类似于url的 alias ,这是我们避免使用整个长url来推送或获取代码的一种方式。在上面的示例中,它称为git remote -v

那么, origin 是什么?

基本上起源是克隆远程存储库时Git用于远程存储库的默认简称。因此,这只是默认

在许多情况下,您将具有指向本地存储库中的多个远程存储库的链接,并且每个存储库都有一个不同的简称。

最后一个问题,为什么我们不只是使用相同的名称?

我将用另一个示例回答这个问题。假设我们有一个朋友分叉了我们的远程存储库,以便他们可以帮助我们进行项目。并假设我们希望能够从其远程存储库中获取代码。我们可以使用命令origin来添加到我们本地存储库中远程存储库的链接。

enter image description here

在上图中,您可以看到我使用简称git remote add <shortname> <url>来指代朋友的远程存储库。您还可以看到两个远程存储库都具有相同的项目名称friend,这为我们提供了一个理由,为什么远程服务器中的远程存储库名称与本地存储库中的短名称应该不相同! >

有一个非常有用的video?解释了here可以找到的所有内容。

答案 7 :(得分:0)

你可以使用 使用 GitHub 或 GitLab 代替 origin

对于 github 代替 origin 使用 github

git remote add github https://github.com/repository_name.git
git push github branchname

对于 gitlab 代替原始使用 gitlab

git remote add gitlab https://github.com/repository_name.git
git push gitlab branchname

答案 8 :(得分:0)

您可以按照以下步骤将更改从现有存储库推送到新远程。

cd existing_repo

重命名您当前的遥控器(可选)

git remote rename origin old-origin

添加新的遥控器(此处使用原点)

git remote add origin https://github.com/repository_name.git

现在您可以将代码推送到新的 origin 遥控器

git push -u origin --all
git push -u origin --tags