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 (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 tri开发者_运维百科es 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%\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.
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
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.
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
精彩评论