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:
This answer is different from the prior answers because it avoids the needless and asymmetric use of the --push
option in the set-url
command of git remote. In this way, both URLs are symmetric in their configuration. For skeptics, the git config as shown by cat ./.git/config
looks different with versus without this option.
git clone git@github.com:myuser/myrepo.git
git remote set-url --add origin git@gitlab.com:myuser/myrepo.git
push
:$ git remote -v
origin git@github.com:myuser/myrepo.git (fetch)
origin git@github.com:myuser/myrepo.git (push)
origin git@gitlab.com:myuser/myrepo.git (push)
$ git config --local --get-regexp ^remote\..+\.url$
remote.origin.url git@github.com:myuser/myrepo.git
remote.origin.url git@gitlab.com:myuser/myrepo.git
git push
To delete an added URL:
git remote set-url --delete origin git@gitlab.com:myuser/myrepo.git
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.
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.
I took the liberty to expand the answer from nona-urbiz; just add this to your ~/.bashrc:
git-pullall () { for RMT in $(git remote); do git pull -v $RMT $1; done; }
alias git-pullall=git-pullall
git-pushall () { for RMT in $(git remote); do git push -v $RMT $1; done; }
alias git-pushall=git-pushall
Usage:
git-pullall master
git-pushall master ## or
git-pushall
If you do not provide any branch argument for git-pullall then the pull from non-default remotes will fail; left this behavior as it is, since it's analogous to git.
Since git 1.8 (October 2012) you are able to do this from the command line:
git remote set-url origin --push --add user1@repo1
git remote set-url origin --push --add user2@repo2
git remote -v
Then git push
will push to user1@repo1, then push to user2@repo2.
Adding new remote
git remote add upstream https://github.com/example-org/example-repo.git
git remote -vv
Fetch form multiple locations
git fetch --all
Push to locations
git push -u upstream/dev