Git Tips: Push to multiple git repo in one command
I’m currently in the process of cleaning my presences on various git forges, sourcehut being my main one now, but I also use(d) github, gitlab and codeberg too. Now that my strategy around repo management is clearer (more on that later), I do have some projects where I still need to push code on multiple forges.
Main reason being that I also have a private Forgejo instance on my homelab only reachable from my home network. I’ll explain the reason in a later post about my git repo management, but that means that every repo will have at least 2 remote repositories to push to: sourcehut and this private forgejo instance.
I also want some repo to stay on codeberg as their main repo, but at the same I want everything on sourcehut (even if private / unlisted), so that I know everything is there and can be made public / private / hidden depending on what is the “source of truth”.
So, here we are with a quick post, more as a reminder, about publishing code to multiple git remote repository. Let’s take this website for example (repository called website on sourcehut and Blog on my personal git instance).
By default, I already have 2 remotes I’ve added with git remote add <nameOfRemote> <urlOfRemoteRepo>:
In the project directory:
git remote -v
Returns for example:
local git@gitssh.bacardi55.local:bacardi55/Blog.git (fetch)
local git@gitssh.bacardi55.local:bacardi55/Blog.git (push)
srht git@git.sr.ht:~bacardi55/website (fetch)
srht git@git.sr.ht:~bacardi55/website (push)
To publish on both, one could do it like this:
git push local main && git push srht main
And that’s what I’ve been using at first. I even created a small gitpushall alias in my ~/.zshrc:
alias gitpushall="git push local main && git push srht main"
Which works fine because I follow the same naming convention for remote repo for all my projects. But I thought it wasn’t the “right” way and looked for a more “git-y” approach. Turns out, there is one as multiple push url can be added to a remote (but only one pull).
I left the remote local and srht as is, and added a new remote called all. This allow me to still decide to push to specific remote if I decide too (eg: pushing on my local git repo to avoid a build to be started on sourcehut CI while waiting for other commits).
To add a new all remote:
git remote add all git@gitssh.bacardi55.local:bacardi55/Blog.git
It still need a default URL, so I choose my local one as the default. Then, we are going to use the set-url remote command to add remote URLs to our repository.
Warning: The first run of git remote set-url… will remove the default URL and use the provided one as default. If you want the one entered in the command above to stay the first one, reuse it first!
git remote set-url --add --push all git@gitssh.bacardi55.local:bacardi55/Blog.git
git remote set-url --add --push all git@git.sr.ht:~bacardi55/website
Running git remote -v will now show the all remote with the 2 push URLs:
all git@gitssh.bacardi55.local:bacardi55/Blog.git (fetch)
all git@gitssh.bacardi55.local:bacardi55/Blog.git (push)
all git@git.sr.ht:~bacardi55/website (push)
local git@gitssh.bacardi55.local:bacardi55/Blog.git (fetch)
local git@gitssh.bacardi55.local:bacardi55/Blog.git (push)
srht git@git.sr.ht:~bacardi55/website (fetch)
srht git@git.sr.ht:~bacardi55/website (push)
Then, git push as usual but with the all remote:
git push all <branchName>
Your code should be pushed in “all” remote repository now :).