14

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 (e.g. 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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
deterb
  • 3,994
  • 1
  • 28
  • 33
  • 1
    I don't think Git supports multiple URLs for remote **yet** (fetch from first responding URL, push to all URLs), so you have to use one remote per URL currently. – Jakub Narębski Apr 16 '10 at 08:08
  • 1
    possible duplicate of [pull/push from multiple remote locations](http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations) – RedX Nov 06 '13 at 09:36
  • See http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations second answer – RedX Nov 06 '13 at 09:37
  • 1
    @RedX thanks for pointing me at that answer. While the question isn't a duplicate from the perspective I wrote this from, which was giving a single remote repository multiple hosts to try and resolve from, not adding multiple hosts. The answer may solve it, and things may have changed since then. I'll have to poke around again with this again this weekend to see if that solution actually resolves the issue; thanks. – deterb Nov 06 '13 at 18:14

5 Answers5

2

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%\system32\drivers\etc\hosts):

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Mark Stickley
  • 1,000
  • 1
  • 12
  • 28
  • Don't have access to etc/hosts unfortunately – deterb Apr 07 '13 at 14:46
  • @deterb What do you mean "don't have access? There should be the equivalent of a hosts file regardless of which operating system you are running: http://en.wikipedia.org/wiki/Hosts_%28file%29#Location_in_the_file_system – IQAndreas Aug 12 '14 at 19:57
  • @IQrandreas It's probably a permissions restriction -- unable to _edit_ the hosts file. – florisla Jun 17 '22 at 13:06
1

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

Abhishek Anand
  • 3,789
  • 2
  • 21
  • 29
1

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.

Aren
  • 54,668
  • 9
  • 68
  • 101
1

As the other answers don't address the original OP question, here is mine:

Create a new remote named all

git remote add all git@github.com:namespace/repo-name.git

Change the remote URL to be a push url only

git remote set-url --add --push all git@github.com:namespace/repo-name.git

Add new push URLs to it

git remote set-url --add --push all git@gitlab.com:namespace/repo-name.git

Of course, the namespace and the repo-name in each remote doesn't have to be the same.

Also, push URLs have a limitation: as the name implies, they are push only. You can't pull from several urls at once. However, you can fetch them all at the same time using git fetch --all, and you can also pull from them one at a time.

If you need more informations, you can look at the documentation here: https://git-scm.com/docs/git-remote

Antoine Viallon
  • 314
  • 4
  • 12
0

I would suggest using some other remote as the sync point between your laptop and other computer(s) that you use.

e.g. github.

If you want to keep it private, try unfuddle or codaset.

hasen
  • 161,647
  • 65
  • 194
  • 231
  • I would love to be able to do this, but I'd need to set up a git server on a Window's box on a fairly locked down IT infrastructure to pull it off. Pushing to a repository outside of the intranet is not allowed. – deterb Apr 16 '10 at 18:48