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

为君一笑 提交于 2019-12-06 04:42:57

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/24719926/git-using-multiple-remotes-to-track-the-same-branch-and-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!