Search code examples
gitnetwork-programmingbranchlan

Git - Using multiple remotes to track the same branch and server


I am trying to set up a Git repo on a remote server of mine, that I am sharing with someone else. Only thing is, this is located in my LAN, which I am not always a part of. I would like to be able to have 2 remotes using the same branch, all synchronized, almost like a symlink (but with configs).

To make things harder, if I try to use the external IP as a remote while in the LAN, it will fail, as that maps to my router's own internal IP. I would like to be able to do git push/pull lan to push while in LAN, and git push/pull wan when not, and ensure neither complain about anything relating to the two of them being separate.

I would like to also ensure they use the same data for syncing between them, as the destination server is the same in either case. I have some experience with Git, but not enough to be able to do this and be sure that it will work as planned.

I do not want to try to sync both of them at once by setting 2 remote URLs for the one branch, as it will just make pushing/pulling very slow because of timeouts.

Assume I have set up LAN already, is up to date, and has an initial commit already, and WAN is not yet set up.

Say the server's internal IP is myserver.lan, and the external one is mydomain.org, how would I go about this?


Solution

  • As I commented below, one can add to a local repo as many remotes as you need:

    git remote add upstream1 /url/first/repo
    git remote add upstream2 /url/first/repo
    

    Then a git push can select the right remote to use:

    git push upstream1
    # or
    git push upstream2
    

    The easiest would be add a script which would:

    • test for the right url (with, for instance, git ls-remote /url/to/remote/repo)
    • replace origin with the url that works in the current environment

      git remote set-url origin /url/that/works
      

    That way, you always have just one remote to manage: the default one called 'origin'.
    But each time you are switching environment, your script can update origin url.