multiple git remotes

I would like to manage a project across multiple remote git repositories, specifically, a public github repository and my own private repositories.

Fortunately, git supports as many remote repositories as you need. When you clone a repository, there will be a default remote called "origin", i.e.,

$ git clone git@github.com:timwarnock/dotfiles.git
...
$ cd dotfiles
$ git remote -v
origin	git@github.com:timwarnock/dotfiles.git (fetch)
origin	git@github.com:timwarnock/dotfiles.git (push)

Adding additional remotes is trivial, i.e.,

$ git remote add bob https://example.com/bob/dotfiles
$ git remote add ann https://example.com/ann/dotfiles
$ git remote add pat https://example.com/pat/dotfiles

Now, when we look at the remotes for our repository we'll see the new entries,

$ git remote -v
origin	git@github.com:timwarnock/dotfiles.git (fetch)
origin	git@github.com:timwarnock/dotfiles.git (push)
bob	https://example.com/bob/dotfiles (fetch)
bob	https://example.com/bob/dotfiles (push)
ann	https://example.com/ann/dotfiles (fetch)
ann	https://example.com/ann/dotfiles (push)
pat	https://example.com/pat/dotfiles (fetch)
pat	https://example.com/pat/dotfiles (push)

If we want to pull from Ann's repository and merge it into our local master branch,

$ git pull ann master

And then if we wanted to push those changes to Bob's repository,

$ git push bob master

We can also rename and remove remotes, i.e.,

$ git remote rename bob bobbie
...
$ git remote remove bobbie

In practice, we may not want to be constantly merging everything into a local master, instead, we may want to investigate the code before any merges. This can be done easily. I prefer to use tracked branches, as follows,

$ git checkout -b bob-master
$ git remote add bob https://example.com/bob/dotfiles
$ git fetch bob master
$ git --set-upstream-to=bob/master

We can now inspect the bob-master branch and merge manually as we see fit,

$ git checkout bob-master
$ git pull
...
$ git checkout master
$ git diff bob-master
...
This entry was posted in git. Bookmark the permalink.