A Superior Git Remote

In the past, whenever I have needed to push to multiple origins for a git repository, I have added another remote:

git remote add gh [email protected]:WebInspectInc/test

This works, and is how you're “supposed” to do it. But I often times dislike this solution.

This is because anytime you push, you have to push to multiple origins, rather than one. No longer can you just run git push, you have to run git push origin and git push origin2. Not only is this more work, it's also more mental overhead, it forces you to think harder about every push you make, even in repos with only one origin. It complicates what should be a simple process.

I should note that I'm specifically talking about pushing to remotes that should always be in sync. If you have "dev" and "live" remotes, then that's fine, you don't always want those to be in sync. But if you have a dev server and a Github repo, like I do on many projects, you probably always want those two repos to stay in sync automatically.

One potential solution here is to set up an alias that pushes to your dev origins. For example, you could decide git p will be your universal "push" alias, and you can set that up like so:

# ~/.gitconfig
[alias]
p = push dev && push gh

I can't recommend this method though. It's good to think about using custom aliases to improve your workflow, but this is a bit too fragile: you have to either make sure to name the remotes the same every time, or modify this alias for every git repo. It's a little better than pushing to two remotes every time, but only a little.

Another method, which would be a little more flexible, would be to add an alias that pushes to all remotes. This could be dangerous, could also be useful:

git config --global alias.pushall '!git remote | xargs -L1 git push --all'

After running that command you should be able to run git pushall and it will push to all origins. Handy! Potentially risky, but handy nonetheless.

A better solution (for me)

The biggest problem with these alias solutions is the lack of precision. I probably have anywhere from 20-50 git repositories on my computer right now, and they're all different. I don't want to have to think about how to push each repo, and aliases just don't have the finesse I need.

Turns out, there's another feature of git that I only just learned about, and it's amazing. It does essentially exactly what I want.

Here's what I do now: when I have multiple "dev" remotes to push to, I don't add another remote. I add another push url. Git allows you to add multiple "push urls" to a single remote. It sounds kind of wacky, but it works. Add more than one push url to a single remote, and git will seamlessly push to all of them whenever you run push.

Whether you already have a remote set up or not, you'll need to run two commands to get this done:

git remote set-url --push --add origin https://origin1.website
git remote set-url --push --add origin https://origin2.website

Note that you'll need to run both of these commands even if you already used one of these urls in your origin, because both urls need to exist as "push urls". Once you've done this, you should be able to run git push and it will push to both places!

I find this to be the best solution to this problem. Sure you have to set it up for each repo, but then you never have to think about it again. Then I can stick to my beloved "git push" without thinking about it, and git handles the rest.

← Home