283

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.
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
  • 11
    You can have as many remote repositories as you want, but you must give them different names. – Felix Kling Jul 27 '12 at 15:16
  • The repository name is different in this case. Is the the word "origin" a name that can be changed? – Chris Dutrow Jul 27 '12 at 15:19
  • 4
    Yep, `git remote rename `: http://linux.die.net/man/1/git-remote. But you can also name the Github repository differently... – Felix Kling Jul 27 '12 at 15:20
  • 1
    possible duplicate of [pull/push from multiple remote locations](http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations) – AD7six Jul 27 '12 at 16:44
  • And is it possible to make one of the remotes the default so I'm not asked to explicitly write the name of that remote everytime I'm pushing? – 40detectives Oct 09 '18 at 08:16
  • Adding to comment by @FelixKling : You can also have same URL mapped to different remotes meaning that the `` can be added as two different remotes - `origin` and `upstream` (you can give any name you want). – Srishti Gupta Mar 08 '20 at 14:20

9 Answers9

404

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
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 5
    As indicated by the answer with the highest votes for the question that this one possibly duplicates - a remote can refer to multiple repositories. Therefore you *can* push to one remote and update two or more repositories, not clear if that's the OP's goal though. – AD7six Jul 28 '12 at 19:49
  • What are some practical examples of why you would want to do this, though? Why would you have one directory pointed to several repositories? Each repository usually has it's own dedicated dir – Daniel May 17 '17 at 07:23
  • 4
    @mightyspaj Pretty much everybody who deploys to Heroku has a remote called `origin` and a remote called `heroku`. – user229044 May 17 '17 at 10:30
  • @mightyspaj, Or if your team is making use of forks and you sometimes want to access them. We do that in our team working with teams in different UTC so that when we approve PRs, we can rebase/push on there behalf by using there fork and are able to merge code now, not tomorrow). – Benoit Drapeau Oct 20 '17 at 03:04
  • Is there a way to push all local branches at once to the new remote? – Tanasis Jan 31 '18 at 10:34
  • Probably, it would be nice to add `git branch -vv --list other-branch` as last command, to show the effect of `git branch --set-...` (see also: [Find out which remote branch a local branch is tracking](https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking)) – Murmel Nov 21 '18 at 10:15
  • This answer is WRONG, you can have multiple remotes for push with `origin`, see https://stackoverflow.com/a/14290145/1614973 – Dmitri Zaitsev May 08 '20 at 06:23
130

Of note it is possible to have origin push to more than one git repository server at a time.

One 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
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 8
    For more thoughts on this, see [this quesion](http://stackoverflow.com/a/12795747/1116842). – moi Jul 31 '16 at 08:59
  • 3
    Ok, so how do I delete one of these origins without deleting all of them? – Michael Dec 01 '17 at 23:26
  • @Michael You can edit the `./git/config` file and remove the url file under [remote "origin"] section – Pini Cheyni Jun 05 '18 at 16:15
  • 8
    @Michael `git remote set-url --delete origin ssh://git@bitbucket.org/user/myproject.git` – Afrig Aminuddin Jul 30 '18 at 04:05
  • Must all remotes have the same commit history? What if I want to push certain commits to one remote and not the other – Qasim May 13 '19 at 06:07
  • 2
    Hey, could someone please mark this as the correct answer? – enanone Mar 17 '21 at 07:50
  • The original questioneer can do so. The following link pretty much sums it up. ``Only the questioner can really say if one of the posts answers their question. Not all questioners bother `` https://sharepoint.meta.stackexchange.com/a/805 – CodeAsm Dec 04 '22 at 20:44
66

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
    
Michael
  • 8,362
  • 6
  • 61
  • 88
dihardmg
  • 761
  • 5
  • 5
16
git remote set-url --add --push origin git@github.com:user/my-project.git
git remote set-url --add --push origin git@bitbucket.org:user/my-project.git

Now you have 2 origins.

Ostap Brehin
  • 3,240
  • 3
  • 25
  • 28
13

A local repository can be linked to multiple remote repositories.

However only one of those links can be called origin. The rest of the links need to have different names.

Therefore in order to properly answer this questions we need to understand what origin is.

Let me explain with an example.

Supposed you have a remote repository called amazing-project and then you clone that remote repository to your local machine so that you have a local repository. Then you would have something like what you can see in the diagram below:

enter image description here

Because you cloned the repository. The remote repository and the local repository are linked.

If you run the command git remote -v it will list all the remote repositories that are linked to your local repository. There you will see that in order to push or fetch code from your remote repository you will use the shortname 'origin'. enter image description here

Now, this may be a bit confusing because in GitHub (or the remote server) the project is called 'amazing-project'. So why does it seem like there are two names for the remote repository?

enter image description here

Well one of the names that we have for our repository is the name it has on GitHub or a remote server somewhere. This can be kind of thought like a project name. And in our case that is 'amazing-project'.

The other name that we have for our repository is the shortname that it has in our local repository that is related to the URL of the repository. It is the shortname we are going to use whenever we want to push or fetch code from that remote repository. And this shortname kind of acts like an alias for the url, it's a way for us to avoid having to use that entire long url in order to push or fetch code. And in our example above it is called origin.

So, what is origin?

Basically origin is the default shortname that Git uses for a remote repository when you clone that remote repository. So it's just the default.

In many cases you will have links to multiple remote repositories in your local repository and each of those will have a different shortname.

So final question, why don't we just use the same name?

I will answer that question with another example. Suppose we have a friend who forks our remote repository so they can help us on our project. And let's assume we want to be able to fetch code from their remote repository. We can use the command git remote add <shortname> <url> in order to add a link to their remote repository in our local repository.

enter image description here

In the above image you can see that I used the shortname friend to refer to my friend's remote repository. You can also see that both of the remote repositories have the same project name amazing-project and that gives us one reason why the remote repository names in the remote server and the shortnames in our local repositories should not be the same!

There is a really helpful video that explains all of this that can be found here.

Anna Skoulikari
  • 1,281
  • 10
  • 9
4

You can use Instead of origin use GitHub or GitLab

for github in place of origin use github

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

for gitlab in place of origin use gitlab

git remote add gitlab https://github.com/repository_name.git
git push gitlab branchname
MD SHAYON
  • 7,001
  • 45
  • 38
4

You can follow the following steps to push your changes in new remote from existing repository.

cd existing_repo

Rename your current remote (optional)

git remote rename origin old-origin

Add new remote (used origin here)

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

Now you can push your code to the new origin remote

git push -u origin --all
git push -u origin --tags
Animesh Rawat
  • 197
  • 3
  • 9
3

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
dinith jayabodhi
  • 531
  • 2
  • 8
  • 19
3
git remote add origin2 https://github.com/Company_Name/repository_name.git

and for push use:

git push -u origin2 master
Pingolin
  • 3,161
  • 6
  • 25
  • 40