git 一个项目可以有多个起源吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11690709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:14:32  来源:igfitidea点击:

Can a project have multiple origins?

gitgithubrepository

提问by Chris Dutrow

Can a project have two (or more) "origins" in Git?

一个项目在 Git 中可以有两个(或更多)“起源”吗?

I would like to push a single project to both githuband a Herokuserver.

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

Specifically, this error appears when adding the github repository:

具体是在添加github仓库时出现这个错误:

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

回答by meagar

You can have as many remotesas you want, but you can only have one remote named "origin". The remote called "origin" is not special in any way, except that it is the default remote created by Git when you clone an existing repository. You can configure a second remote, push to/pull from that remote, and setup some branches to track branches from that remote instead of origin.

您可以拥有任意数量的遥控器,但您只能拥有一个名为“origin”的遥控器。名为“origin”的远程没有任何特殊之处,除了它是当您克隆现有存储库时由 Git 创建的默认远程。您可以配置第二个远程,从该远程推送/拉取,并设置一些分支以跟踪来自该远程而不是源的分支。

Try adding a remote called "github" instead:

尝试添加一个名为“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

回答by Josh_at_Savings_Champion

As a side note for anyone stumbling upon this question later, it is possible to have origin push to more than one git repository server at a time.

作为稍后遇到此问题的任何人的旁注,可以一次将源推送到多个 git 存储库服务器。

You can achieve this by using the following command to add another URL to the origin remote.

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

git remote set-url --add origin ssh://[email protected]/user/myproject.git

回答by dihardmg

Here's a sample project with multiple remotes, GitHub & GitLab:

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

  1. Add remote repo for GitHub

    $ git remote add github https://github.com/Company_Name/repository_name.git
    
  2. Add remote repo for GitLab

    $ git remote add gitlab https://gitlab.com/Company_Name/repository_name.git
    
  3. Now you have multiple remotes in the project. Double check with 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. How do you push to multiple repositories?

    $ git push github && git push 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
    

回答by dinith jayabodhi

you can add another remote account to your repository through giving different name instead of origin. You can use name such as origin2. so your git command can be modified as

您可以通过提供不同的名称而不是来源将另一个远程帐户添加到您的存储库。您可以使用名称,例如 origin2。所以你的 git 命令可以修改为

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

回答by Lovepreet Kaur

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

and for push use:

并用于推送使用:

git push -u origin2 master