Can a project have multiple origins?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 10:07:46

问题


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

I would like to push a single project to both github and a Heroku server.

Specifically, this error appears when adding the github repository:

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

回答1:


You can have as many remotes as 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.

Try adding a remote called "github" instead:

$ 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



回答2:


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.

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

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



回答3:


Here's a sample project with multiple remotes, 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
    



回答4:


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

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



回答5:


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

and for push use:

git push -u origin2 master


来源:https://stackoverflow.com/questions/11690709/can-a-project-have-multiple-origins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!