How can I pull/push from multiple remote locations?

Viewed 376219

The short: is there a way to have a git repo push to and pull from a list of remote repos (rather than a single "origin")?

The long: I often have a situation when I'm developing an app in multiple computers, with different connectivity – say a laptop while on transit, a computer "A" while I'm in a certain location, and another computer "B" while on another. Also, the laptop might have connectivity with only either "A" or "B", and sometimes both.

What I would like to is for git to always "pull" from and "push" to all the computers it can currently connect to, so it's easier to jump from one machine to the other and continue working seamlessly.

16 Answers

You can configure multiple remote repositories with the git remote command:

git remote add alt alt-machine:/path/to/repo

To fetch from all the configured remotes and update tracking branches, but not merge into HEAD, do:

git remote update

If it's not currently connected to one of the remotes, it will take time out or throw an error, and go on to the next. You'll have to manually merge from the fetched repositories, or cherry-pick, depending on how you want to organize collecting changes.

To fetch the master branch from alt and pull it into your current head, do:

git pull alt master

So in fact git pull is almost shorthand for git pull origin HEAD (actually it looks in the config file to determine this, but you get the idea).

For pushing updates, you have to do that to each repo manually.
A push was, I think, designed with the central-repository workflow in mind.

I added these aliases to my ~/.bashrc:

alias pushall='for i in `git remote`; do git push $i; done;'
alias pullall='for i in `git remote`; do git pull $i; done;'

You can add remotes with:

git remote add a urla
git remote add b urlb

Then to update all the repos do:

git remote update
  1. Clone from the first URL:
git clone git@github.com:myuser/myrepo.git
  1. Review the current remote:
$ git remote -v
origin  git@github.com:myuser/myrepo.git (fetch)
origin  git@github.com:myuser/myrepo.git (push)
  1. Add the second remote for origin:
git remote set-url --add origin git@bitbucket.org:myuser/myrepo.git
  1. Confirm that both remotes are listed for push:
$ git remote -v
origin  git@github.com:myuser/myrepo.git (fetch)
origin  git@github.com:myuser/myrepo.git (push)
origin  git@bitbucket.org:myuser/myrepo.git (push)

$ git config --local --get-regexp ^remote\..+\.url$
remote.origin.url git@github.com:myuser/myrepo.git
remote.origin.url git@bitbucket.org:myuser/myrepo.git
  1. Push to all URLs in sequence:
git push

To delete a remote:

git remote set-url --delete origin git@bitbucket.org:myuser/myrepo.git

Well, if you are looking for a way to push multiple remotes simultaneously, you might be aware of git well enough. We call remote repositories as remotes in git. Pushing changes to remotes would be part of usually development cycle.

Sometimes, you may need to push changes to multiple remotes like GitHub, bitbucket, etc. To do so, you can follow the given instructions.

List your existing remotes

You can list all available remotes using the following command.

git remote -v

Suppose you don’t have already any(other) remote configured. You can do this by using the git remote.

git remote add  remote_name  remote_url

Example:

git remote add github https//github.com/path/to/repo

Usually, we push changes by addressing remote name by default origin something like git push origin. You can configure group multiple remotes and give it a name. So you push to all those remotes by referring to that name.

You can add multiple remotes by using git remote or git config commands or editing the config file.

As git can group multiple remotes, you can follow any of the following ways to configure multiple remotes to push simultaneously(no need all).

Add remotes using git remote

You can set multiple remote URLs to a single remote using git remote.

If you don’t have a remote named 'all' already, create it using git remote add then use git remote set-url –add to add a new URL to the existing remote.

git remote add all <remote URL>

Then

git remote set-url  -–add  all  <another remote URL>

You can cross-check added new remotes using git remote -v.

(OR)

Group multiple remotes using git config

The git config command is used to configure git parameters. It will edit the .git/config file as given input.

git config –add remote.all.url  https//domain.com/repo.git
git config –add remote.all.url  ssh://user@host/repos/repo.git

Note without –add option command will replace existing remote URL. You can verify the updated config at .git/config.

(OR)

Edit file .git/config to add remote and multiple remote URLs if you know the configuration format.

Now you can push multiple remotes simultaneously by referring to the remote name with multiple remote URLs assigned.

git push all master

You can always push to multiple remote repositories without grouping them using formal bash syntax.

git push server master && git push github master

You'll need a script to loop through them. Git doesn't a provide a "push all." You could theoretically do a push in multiple threads, but a native method is not available.

Fetch is even more complicated, and I'd recommend doing that linearly.

I think your best answer is to have once machine that everybody does a push / pull to, if that's at all possible.

For updating the remotes (i.e. the pull case), things have become easier.

The statement of Linus

Sadly, there's not even any way to fake this out with a git alias.

in the referenced entry at the Git mailing list in elliottcable's answer is no longer true.

git fetch learned the --all parameter somewhere in the past allowing to fetch all remotes in one go.

If not all are requested, one could use the --multiple switch in order to specify multiple remotes or a group.

Adding the all remote gets a bit tedious as you have to setup on each machine that you use.

Also, the bash and git aliases provided all assume that you have will push to all remotes. (Ex: I have a fork of sshag that I maintain on GitHub and GitLab. I have the upstream remote added, but I don't have permission to push to it.)

Here is a git alias that only pushes to remotes with a push URL that includes @.

psall    = "!f() { \
    for R in $(git remote -v | awk '/@.*push/ { print $1 }'); do \
    git push $R $1; \
    done \
    }; f"
Related