A situation that happens often – you’re working on a repository (GitHub for example) also want your commits automatically mirrored to anther repository.
The obvious solution is to add a second remote, but that means having to constantly remember to push to both remotes.
After playing around with the idea of aliases and hooks, I fell upon a little-known Git feature – secondary fetch and push URLs.
We can add additional fetch and push URLs for a remote by using the reite set-url
For example, to add https://my-backup-remote as a remote for the current
git remote set-url –add –push origin https://my-backup-remote
Taking a peek at the .config fileL
[remote “origin”]
url = https://github.com/alanapz/snorlax.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = https://github.com/alanapz/snorlax.git
pushurl = \\\\192.168.1.40\\public\\projects\\snorlax
And
C:\dev\adista\snorlax>git remote show origin
* remote origin
Fetch URL: https://github.com/alanapz/snorlax.git
Push URL: https://github.com/alanapz/snorlax.git
Push URL: \\192.168.1.40\public\projects\snorlax
HEAD branch: master
Remote branch:
master tracked
Local ref configured for ‘git push’:
master pushes to master (up to date)
C:\dev\adista\snorlax>git push origin head -v
Pushing to https://github.com/alanapz/snorlax.git
To https://github.com/alanapz/snorlax.git
= [up to date] head -> master
updating local tracking ref ‘refs/remotes/origin/master’
Everything up-to-date
Pushing to \\192.168.1.40\public\projects\snorlax
To \\192.168.1.40\public\projects\snorlax
= [up to date] head -> master
updating local tracking ref ‘refs/remotes/origin/master’
Everything up-to-date
Notice that fetches are not affected – we fetch from only the fetch URL (perfect – it doesn’t really make sense to fetch from our backup remote).
C:\dev\adista\snorlax>git fetch -v From https://github.com/alanapz/snorlax = [up to date] master -> origin/master
Quick, easy & dirty !