Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pull/push from multiple remote locations?

Tags:

git

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.

like image 783
Zorzella Avatar asked May 11 '09 18:05

Zorzella


People also ask

Can git push to multiple remotes?

It is easy to synchronize code between multiple git repositories, especially, pushing to multiple remotes. This is helpful when you're maintaining mirrors / copies of the same repository. All you need to do is set up multiple push URLs on a remote and then perform git push to that remote as you usually do.

How do I push all remote branches?

To push the all branches to remote git, we can use the git push command followed by the --all flag and origin.

Does git push push to all remotes?

Running git pushall will now push all branches to all remotes.

How do I fetch all local branches from a remote?

1 Answer. git fetch --all and git pull -all will only track the remote branches and track local branches that track remote branches respectively. Run this command only if there are remote branches on the server which are untracked by your local branches. Thus, you can fetch all git branches.


2 Answers

Doing this manually is no longer necessary, with modern versions of git! See Malvineous's solution, below.

Reproduced here:

git remote set-url origin --push --add <a remote> git remote set-url origin --push --add <another remote> 

Original answer:

This something I’ve been using for quite a while without bad consequences and suggested by Linus Torvalds on the git mailing list.

araqnid’s solution is the proper one for bringing code into your repository… but when you, like me, have multiple equivalent authoritative upstreams (I keep some of my more critical projects cloned to both a private upstream, GitHub, and Codaset), it can be a pain to push changes to each one, every day.

Long story short, git remote add all of your remotes individually… and then git config -e and add a merged‐remote. Assuming you have this repository config:

[remote "GitHub"]     url = [email protected]:elliottcable/Paws.o.git     fetch = +refs/heads/*:refs/remotes/GitHub/* [branch "Master"]     remote = GitHub     merge = refs/heads/Master [remote "Codaset"]     url = [email protected]:elliottcable/paws-o.git     fetch = +refs/heads/*:refs/remotes/Codaset/* [remote "Paws"]     url = [email protected]:Paws/Paws.o.git     fetch = +refs/heads/*:refs/remotes/Paws/* 

… to create a merged‐remote for "Paws" and "Codaset", I can add the following after all of those:

[remote "Origin"]     url = [email protected]:Paws/Paws.o.git     url = [email protected]:elliottcable/paws-o.git 

Once I’ve done this, when I git push Origin Master, it will push to both Paws/Master and Codaset/Master sequentially, making life a little easier.

like image 90
ELLIOTTCABLE Avatar answered Oct 05 '22 03:10

ELLIOTTCABLE


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.

like image 43
araqnid Avatar answered Oct 05 '22 03:10

araqnid