193

Attempting something like git clone git://github.com/ry/node.git will not work, it results in:

Initialized empty Git repository in /home/robert/node/.git/
github.com[0: 207.97.227.239]: errno=Connection timed out
fatal: unable to connect a socket (Connection timed out)

However, cloning over HTTP works fine. So far I've gathered that it's a problem with the protocol, but I'm trying to install cloud9 which is requiring the command

git submodule update --init --recursive

which is trying to use the git:// protocol and failing. Is there a way to change how that command will work or something?

Robert
  • 21,110
  • 9
  • 55
  • 65
  • do you have SSH access? or just HTTP? – Pablo Santa Cruz Feb 03 '11 at 20:51
  • 62
    What is it with people trying to close git questions? According to the FAQ, the scope of SO includes "software tools commonly used by programmers". There are over six thousand git questions here. They belong here. – Cascabel Feb 03 '11 at 20:55
  • 12
    You can get Git to automatically use https:// whenever it sees a git:// URL: `git config --global url.https://.insteadOf git://` – WildlyInaccurate Jan 02 '14 at 14:43
  • As of March 2022 git:// is no longer supported by Github: https://github.blog/2021-09-01-improving-git-protocol-security-github/ – Steffen Wenzel May 26 '22 at 12:03

6 Answers6

442

If this is an issue with your firewall blocking the git: protocol port (9418), then you should make a more persistent change so you don't have to remember to issue commands suggested by other posts for every git repo.

The below solution also just works for submodules which might also be using the git: protocol.

Since the git message doesn't really point immediately to the firewall blocking port 9418, lets try to diagnose this as the actual problem.

Diagnosing the Problem

References: https://superuser.com/q/621870/203918 and https://unix.stackexchange.com/q/11756/57414

There are several tools we can use to determine if the firewall causing our problem - use whichever is installed on your system.

# Using nmap
# A state of "filtered" against port 9418 (git) means
#   that traffic is being filtered by a firewall
$ nmap github.com -p http,git

Starting Nmap 5.21 ( http://nmap.org ) at 2015-01-21 10:55 ACDT
Nmap scan report for github.com (192.30.252.131)
Host is up (0.24s latency).
PORT     STATE    SERVICE
80/tcp   open     http
9418/tcp filtered git

# Using Netcat:
# Returns 0 if the git protocol port IS NOT blocked
# Returns 1 if the git protocol port IS blocked
$ nc github.com 9418 < /dev/null; echo $?
1

# Using CURL
# Returns an exit code of (7) if the git protocol port IS blocked
# Returns no output if the git protocol port IS NOT blocked
$ curl  http://github.com:9418
curl: (7) couldn't connect to host

OK, so now we have determined it is our git port being blocked by a firewall, what can we do about it? Read on :)

Basic URL Rewriting

Git provides a way to rewrite URLs using git config. Simply issue the following command:

git config --global url."https://".insteadOf git://

Now, as if by magic, all git commands will perform a substitution of git:// to https://

What Changes Did This Command Make?

Take a look at your global configuration using:

git config --list

You'll see the following line in the output:

url.https://.insteadof=git://

You can see how this looks on file, by taking a peek at ~/.gitconfig where you should now see that the following two lines have been added:

[url "https://"]
    insteadOf = git://

Want More Control?

Simply use a more complete/specific URL in the replacement. For example, to only have GitHub URLs use https:// instead of git://, you could use something like:

git config --global url."https://github".insteadOf git://github

You can run this command multiple times using different replacements. However, in the event that a URL matches multiple replacements, the longest match "wins". Only a single replacement will be made per URL.

System-Wide Changes for Sysadmins

If you're a Linux Sysadmin and you don't want your users to have to go through the above pains you can make a quick system-wide git configuration change.

Simply edit or add the following contents to /etc/gitconfig and voila your users don't have to worry about any of the above:

[url "https://"]
    insteadOf = git://
Community
  • 1
  • 1
Nathan S. Watson-Haigh
  • 5,043
  • 2
  • 19
  • 19
  • 1
    Works great! No more search and replace. Build scripts just work now. This answer saved me a lot of time. Thanks! – Jeremy Bell May 01 '13 at 13:05
  • 8
    For a little more control of which URL gets converted, you can specify part of URL as well. For example: I have a private internal server 'myserver.lan.example.com' that hosts git repos over SSH (gitlab) but not HTTPS. Therefore, I _must_ use SSH if I want to take advantage of convenient key authentication. I also use repos from Github, but my corporate firewall blocks SSH to Github. I don't want to simply replace all instances of 'git://' with 'https://' as that would break gitlab. The solution is `git config --global url."https://github".insteadOf git://github`. – clayzermk1 Jul 15 '13 at 19:08
  • This will not work in windows 8. I observed that the command is not setting the value in disk.For a --list operation it will print.But if you go to the /etc/gitconfig file it won't have the change. Need to login the bash shell and edit the file manually then try the bower install....then it will work. – sunnychayen Aug 18 '14 at 13:49
  • I normally prefer to modify any settings only for the repository local configuration. This did not work in that case. Seem like you have to use `--global`. – Paidhi Mar 13 '15 at 12:30
  • 2
    I was running git from inside of cygwin and the only way I could get this to work was to do the 'System-Wide Changes for Sysadmins' and adding the 'url.https://.insteadof=git://' changes to the 'C:\Program Files (x86)\Git\etc\gitconfig' file. Thanks for the hint! – Craig Jul 03 '15 at 06:50
  • 4
    To undo this change one can use `git config --global --unset url."https://".insteadOf` – djskinner Aug 25 '15 at 10:37
29

Github provides http(s) access too, which is much less likely to be blocked by your company. To tell the submodule to use that, you can do this:

git submodule init
git config submodule.<name>.url https://github.com/...
git submodule update

This is actually exactly why init and update are separate commands - you can init, customize locations, then update. update --init is just a shortcut for when you don'ot need to customize any URLs.

For anyone else who happens across this, you could of course also use an ssh URL (if your company blocks git:// but not ssh), but in this case the OP presumably doesn't have SSH access to the remote repo.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • Sounds like this will probably work for me, although it seems I'll have to go through each one individually. Was doing https://github.com/ajaxorg/cloud9 specifically if that matters. – Robert Feb 03 '11 at 21:13
  • 4
    @Robert: If there are a lot, you could edit the config file directly and do a search and replace: `sed -i 's@git://github@https://github@' .git/config`. – Cascabel Feb 04 '11 at 00:07
  • Hmm, for some reason they say http:// in the file, but the command still tries git:// – Robert Feb 04 '11 at 23:17
  • 1
    I'm having the same problem described in the OP, but when I use this solution, it still fails, but with a slightly different error. It says "error: while accessing https://... fatal: HTTP request failed" Does anyone have any insight on this? Is my Host blocking something? My other submodules update fine, I am only having trouble with one. – Jo Sprague Sep 21 '12 at 15:11
19

Another option that does not involve touching git config is to enable SSH connections over HTTPS. You can do so by changing your SSH settings to use HTTPS port 443 instead of the regular SSH port 22 for SSH connections.

From GitHub's article about Using SSH over the HTTPS port:

edit the file at ~/.ssh/config, and add this section:

Host github.com
   Hostname ssh.github.com
   Port 443  

After this change, I was able to successfully git push to Github.

When you are back at home you can remove this SSH config change to revert back to default settings.

UrbanConor
  • 162
  • 1
  • 5
  • 16
elpddev
  • 4,314
  • 4
  • 26
  • 47
8

I was also having the same issue for a while. Then I tried changing the git config using the suggested command:

git config --global url."https://".insteadOf git://

which unfortunately did not do the trick for me. I was still having the same problem!

What actually solved my problem at last is, I have reset the remote url of my repository again using the following command:

git remote set-url origin https://github.com/<my_user_name>/<my_repo_name>.git

which was previously like this:

git remote set-url origin git@github.com:<my_user_name>/<my_repo_name>.git

After setting the remote url using https:// instead of git@git.com the problem was resolved for me.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • 2
    I experienced a similar issue. It seems setting the global only affects repos cloned going forward, and does not change any retroactively. – Taylor D. Edmiston May 01 '16 at 22:34
2

Expanding on Nathan's answer above, you can also try the ssh protocol if your corporate firewall is interfering with https. In my case the firewall was blocking git protocol, re-issuing ssl certificates for https and this was breaking bower for me, even with the strict-ssl option turned off. You can do a similar url rewrite for ssh, and create a ssh key/pair as described on github.

 git config --global url."ssh://git@github.com".insteadOf git://github.com

You would also have to turn on the ssh-agent for your git install.

Community
  • 1
  • 1
jhiller
  • 211
  • 2
  • 4
1

it's because the GIT adresse for the node server has changed you have to enter now:

git clone https://github.com/joyent/node

good luck

fmo
  • 37
  • 1