How can I attach multiple urls to a single git remote?

I'm currently using git on windows through a combination of msysgit and Cygwin.

I have a laptop which I move around quite a bit, so it's not on a consistent location. Unfortunately, I don't have a consistent name for it due to the computer name not being resolved on all of the locations I connect to, so I can't just use the computer name as the host for the url (eg git://compname/repo), so I have to use the IP address.

Is there a way I can add multiple urls to pull from for a particular remote? I've seen

git remote set-url --add [--push] <name> <newurl>

as a way to add multiple URLs to a remote, and I can see the updates in the .git/config file, but git only tries to use the first one.

Is there a way to get git to try to use all of the urls? I've tried both git fetch and git remote update, but neither tries anything after the first url.

Note that I haven't tried this on linux yet, and I can't fix the computer name resolution as this is at work.


I think your best best is to set your remote URI to point to computername but then add computername to your hosts file (located at %SystemRoot%system32driversetchosts ):

computername    192.168.100.34
# computername    192.168.100.68

Then you can keep multiple entries for the different IP addresses and comment / uncomment them as needed. No more messing about changing the remote URI on a per-repo basis, just update in one place and then all repos using computername as the URI will use the new location.


I personally do the same thing, what I used is two remotes

origin: location of my repo when at home foriegn: location of my repo when anywhere else (my dns redirect to my house)

then i just push/pull to the appropriate remote, it's a bit messy but it works.


This approach is inspired by @Mark Stickley's answer. It works even if you have two different port numbers (not just hostnames) to access the same machine from two different locations.

You can keep multiple versions of ~/.ssh/config depending on the number of situations.

For example, in my case at home I access my server with hostname as my-fancy-server and port as 22 . At office, the hostname is my-dyndns-name and the port name is XXX .

So I have two version of ~/.ssh/config . At home, it says:

Host home
    my-fancy-server
    Port 22
    User abhishek
    IdentityFile /home/abhishek/.ssh/id_rsa

(please adjust he last two values accordingly. The last one is not needed if you don't use keys) At office, it says

Host home
    my-dyndns-name
    Port XXX
    User abhishek
    IdentityFile /home/abhishek/.ssh/id_rsa

I have a script that adjusts the contents of ~/.ssh/config when I move to a new location. One could automate even this by writing a script which is triggered by changes in the network.

I have only one remote in my git config file which looks like

[remote "origin"]
    url = home:/path/to/repo
    fetch = +refs/heads/*:refs/remotes/origin/*

EDIT : I just found out that I dont have to do all this. I just enabled nat loopback on my router and now I can uniformly access my computer using the external name/port. However in many situations, like a corporate network, messing with the router might not be an option

链接地址: http://www.djcxy.com/p/49780.html

上一篇: 我如何将存储库同步到GitHub和我的内部Git服务器?

下一篇: 如何将多个网址附加到单个git远程?