132

I am setting up my local git project for a remote repository. The remote repository is being served on a non-standard port (4019).

But it doesn't work. Instead I get the following error message:

ssh: connect to host git.host.de:4019 port 22: Connection refused
fatal: The remote end hung up unexpectedly
error: failed to push to 'ssh://root@git.host.de:4019/var/cache/git/project.git'

My local git config is as follows:

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
[remote "origin"]
  url = ssh://root@git.host.de:4019/var/cache/git/project.git
  fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
  remote = origin
  merge = refs/heads/master

(The port and host are placeholders for the actual port and host.)

What is wrong with my git configuration?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
brainfck
  • 9,286
  • 7
  • 28
  • 29
  • 9
    Looks like if you don't explicitly put `ssh://` in front of the url, it thinks it's a different format. so `ssh://example.com:444/etc/` is /etc/ on example.com via port 44. Whereas `example.com:444/etc/` is /444/etc/ on example.com via port 22. – Kzqai Jul 26 '11 at 14:42
  • 5
    @Kzqai remark is important. i.e if you do `git remote set-url origin git@altssh.bitbucket.org:443/yourname/yourrepo/` it wont work. but, if you do `git remote set-url origin ssh://git@altssh.bitbucket.org:443/yourname/yourrepo/` this will work – oak Nov 28 '13 at 13:11
  • For the Google-fu - I had this problem with phabricator when i had it set to a non-default port of 2222. Now, sorted! – user3791372 Feb 21 '17 at 09:36

5 Answers5

148

SSH based git access method can be specified in <repo_path>/.git/config using either a full URL or an SCP-like syntax, as specified in http://git-scm.com/docs/git-clone:

URL style:

url = ssh://[user@]host.xz[:port]/path/to/repo.git/

SCP style:

url = [user@]host.xz:path/to/repo.git/

Notice that the SCP style does not allow a direct port change, relying instead on an ssh_config host definition in your ~/.ssh/config such as:

Host my_git_host
HostName git.some.host.org
Port 24589
User not_a_root_user

Then you can test in a shell with:

ssh my_git_host

and alter your SCP-style URI in <repo_path>/.git/config as:

url = my_git_host:path/to/repo.git/
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
jdpf
  • 1,481
  • 1
  • 9
  • 2
122

If you put something like this in your .ssh/config:

Host githost
HostName git.host.de
Port 4019
User root

then you should be able to use the basic syntax:

git push githost:/var/cache/git/project.git master
Peter
  • 127,331
  • 53
  • 180
  • 211
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 5
    A SSH config might be a workaround, but this got me interested, because man git-push says that the accepted ssh url format is ssh://[user@]host.xz[:port]/path/to/repo.git/ – gnud Oct 13 '09 at 07:50
  • 1
    I'm not sure, it may be a configuration git/ssh version issue because I tried to push to a ssh://fake@localhost:333/fake address and got (as expected) "port 333: Connection refused". – CB Bailey Oct 13 '09 at 08:00
  • 38
    Attention anyone who still needs this. The syntax is `git clone ssh://username@hostname:333/~/repo` for a path relative to your home directory or `git clone ssh://username@hostname:333/path/to/repo` for an absolute path – Brandon Jul 05 '12 at 21:56
  • on a windows machine: c:/windows/system32/drivers/etc/hosts to set a host name if needed (e.g. virtual machines / servers) – user3791372 Feb 21 '17 at 09:36
  • How can this be done if I have an IP and not a host name? I ask because we haven't linked our host name to our ip yet. – ThisIsNotAnId Feb 22 '19 at 23:14
  • In ubuntu, use `sudo nano ~/.ssh/config` to edit the config file. – Muhammad Yasirroni Nov 17 '22 at 03:07
31

Try this

git clone ssh://user@32.242.111.21:11111/home/git/repo.git
Ricky Sahu
  • 23,455
  • 4
  • 42
  • 32
  • 1
    Hi Ricky , it has worked for me with the syntax git clone ssh://user@machine:port/path-to-repo , where you write machine must be user, thanks – rtrujillor Sep 18 '15 at 10:34
9

This avoids your problem rather than fixing it directly, but I'd recommend adding a ~/.ssh/config file and having something like this

Host git_host
HostName git.host.de
User root
Port 4019

then you can have

url = git_host:/var/cache/git/project.git

and you can also ssh git_host and scp git_host ... and everything will work out.

Peter
  • 127,331
  • 53
  • 180
  • 211
8

SSH doesn't use the : syntax when specifying a port. The easiest way to do this is to edit your ~/.ssh/config file and add:

Host git.host.de
  Port 4019

Then specify just git.host.de without a port number.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285