I have two remote git repositories. origin and github
I push my branch devel to both repositories.
git push -u origin devel
git push -u github devel
But then, when I do. git push It would only get pushed to github.
Is there anyway I can set up my two remotes, so that I can push changes to both repositories with one command ?
In recent versions of Git you can add multiple
pushurls for a given remote. Use the following to add twopushurls to yourorigin:So when you push to
origin, it will push to both repositories.UPDATE 1: Git 1.8.0.1 and 1.8.1 (and possibly other versions) seem to have a bug that causes--addto replace the original URL the first time you use it, so you need to re-add the original URL using the same command. Doinggit remote -vshould reveal the current URLs for each remote.UPDATE 2: Junio C. Hamano, the Git maintainer, explained it’s how it was designed. Doing
git remote set-url --add --push <remote_name> <url>adds apushurlfor a given remote, which overrides the default URL for pushes. However, you may add multiplepushurls for a given remote, which then allows you to push to multiple remotes using a singlegit push. You can verify this behavior below:Now, if you want to push to two or more repositories using a single command, you may create a new remote named
all(as suggested by @Adam Nelson in comments), or keep using theorigin, though the latter name is less descriptive for this purpose. If you still want to useorigin, skip the following step, and useorigininstead ofallin all other steps.So let’s add a new remote called
allthat we’ll reference later when pushing to multiple repositories:Then let’s add a
pushurlto theallremote, pointing to another repository:Here
git remote -vshows the newpushurlfor push, so if you dogit push all master, it will push themasterbranch togit://another/repo.gitonly. This shows howpushurloverrides the default url (remote.all.url).Now let’s add another
pushurlpointing to the original repository:You see both
pushurls we added are kept. Now a singlegit push all masterwill push themasterbranch to bothgit://another/repo.gitandgit://original/repo.git.IMPORTANT NOTE: If your remotes have distinct rules (hooks) to accept/reject a push, one remote may accept it while the other doesn’t. Therefore, if you want them to have the exact same history, you’ll need to fix your commits locally to make them acceptable by both remotes and push again, or you might end up in a situation where you can only fix it by rewriting history (using
push -f), and that could cause problems for people that have already pulled your previous changes from the repo.