realm

Git Gud: Push into Two Repositories at Once

October 28, 2024
4 min read
Table of Contents

Abstract

So I just hosted my own Gitea instance a few while back. It’s pretty cool and I’m very happy with how it goes. It has so many things that I can do with it, well, speaking of features, it allows me to do everything GitHub can do, but more. One feature that I like in particular is the ability to mirror repository from other Git hosting services. This is very useful for me as I can have a backup of my repositories in case something bad happens to GitHub.

GitHub in general also has something similar to the mirroring feature called GitHub Imports, except that it’s technically not a mirroring feature. It merely cloned the repository from my Gitea instance into my GitHub account and… that’s it really. So I thought for a bit, “What if I can push my code into both GitHub and Gitea at the same time?” And so I did some research and found out that it’s actually possible to do so. Here’s how you can do it.

Pushing into Two Repositories

At first, I devised a plan that involves writing out a script that will push the code into both repositories by switching the remote URL back and forth. I actually discussed this a bit with my fellow GNU/Weeb peers while also asking how did Linus Torvalds mirror linux repository from lore to GitHub. They told me that Linus uses a script to push the code into both repositories at once. So I explored the idea by writing a script that does just that.

#!/bin/bash
 
git push
git remote set-url origin git@github.com:irvanmalik48/laterano.git
git push
git remote set-url origin git@git.irvanma.eu.org:irvanmalik48/laterano.git
 
echo "Pushed into both repositories."

I saved the script as push.sh and made it executable by running chmod +x push.sh. I then ran the script by executing ./push.sh and voila! The code is now pushed into both repositories. Pretty nice, right? But then, I googled a bit and found out that there’s a better way to do this.

Using Multiple Remote Push URLs

Turns out, you can actually set multiple push URLs for a single repository. This is very useful as you don’t have to switch the remote URL back and forth. I got the idea from this blog post (which is an inspiration for this post anyways, kudos to whoever wrote that post). Here’s how you can do it.

git remote set-url --add --push origin git@git.irvanma.eu.org:irvanmalik48/laterano.git
git remote set-url --add --push origin git@github.com:irvanmalik48/laterano.git

And that’s it! You can now push your code into both repositories by running git push. If you’re still not convinced, you can check the remote URL by running git remote -v and see that there are two push URLs for the origin remote. For me, it looks like this:

lappland ~/Projects/laterano (main) $ git remote -v
origin	git@git.irvanma.eu.org:irvanmalik48/laterano (fetch)
origin	git@git.irvanma.eu.org:irvanmalik48/laterano (push)
origin	git@github.com:irvanmalik48/laterano (push)

Conclusion

So that’s how you can push your code into two repositories at once. I hope you find this post useful and if you have any questions or suggestions, feel free to email me or contact me on my handles available down the footer (well, the comment system ain’t ready yet, so…). Thanks for reading and see you in the next post!