使用具有非标准端口的远程存储库


125

我正在为远程存储库设置本地git项目。远程存储库正在非标准端口上提供(4019)。

但这是行不通的。相反,我收到以下错误消息:

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'

我的本地git配置如下

[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

(端口和主机是实际端口和主机的占位符。)

我的git配置有什么问题?


8
看起来,如果您未明确将其放在ssh://url之前,它会认为这是一种不同的格式。所以ssh://example.com:444/etc/是/ etc / example.com上通过端口44而example.com:444/etc/是/ 444的/ etc / example.com上经由端口22
Kzqai

5
@Kzqai的评论很重要。即,如果您这样做git remote set-url origin git@altssh.bitbucket.org:443/yourname/yourrepo/将无法正常工作。但是,如果您这样做,git remote set-url origin ssh://git@altssh.bitbucket.org:443/yourname/yourrepo/将起作用
橡树

对于Google Fu,当我将phabricator设置为2222的非默认端口时,我遇到了这个问题。
user3791372

Answers:


116

如果您将这样的内容放入您的.ssh/config

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

那么您应该能够使用基本语法:

git push githost:/var/cache/git/project.git master

4
SSH配置可能是一种解决方法,但这引起了我的兴趣,因为man git-push说可接受的ssh网址格式为ssh:// [user @] host.xz [:port] /path/to/repo.git /
gnud

1
我不确定,这可能是配置git / ssh版本的问题,因为我试图推送到ssh:// fake @ localhost:333 / fake地址并得到(如预期的那样)“端口333:连接被拒绝”。
CB Bailey 2009年

35
注意仍需要此功能的任何人。语法适用git clone ssh://username@hostname:333/~/repo于相对于您的主目录的路径,也git clone ssh://username@hostname:333/path/to/repo适用于绝对路径
Brandon Wamboldt

在Windows机器上:c:/ windows / system32 / drivers / etc / hosts设置主机名(如果需要)(例如虚拟机/服务器)
user3791372

如果我有IP而不是主机名,该怎么办?我问是因为我们还没有将主机名链接到我们的IP。
ThisIsNotAnId

129

可以<repo_path>/.git/config使用完整URL或类似SCP的语法指定基于SSH的git访问方法,如http://git-scm.com/docs/git-clone中所指定:

网址样式:

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

SCP风格:

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

请注意,SCP样式不允许直接更改端口,而是依靠ssh_config您中的主机定义,~/.ssh/config例如:

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

然后,您可以使用以下命令在shell中进行测试:

ssh my_git_host

并将您的SCP样式URI更改<repo_path>/.git/config为:

url = my_git_host:path/to/repo.git/

1
Soooooo ...可能真正需要的是在url前面添加ssh://。
卡扎伊2011年

1
没有意识到您可以指定ssh url或scp(默认值)不允许端口号。很高兴知道。
Powerlord 2014年

1
scp实际上允许端口号(但选项密钥必须为大写P):scp -P 2020 file/to/copy user@host:path/to/copy/file/to
Drew

1
对我来说,添加ssh://到我的作品中就足够了url。如果缺少协议,则git使用默认端口:22。谢谢。
Marek Podyma '18

28

试试这个

git clone ssh://user@32.242.111.21:11111/home/git/repo.git

1
嗨,Ricky,它已经对我有用git clone ssh:// user @ machine:port / path-to-repo语法,您在其中写入计算机必须是用户,谢谢
rtrujillor

9

这避免了您的问题,而不是直接解决问题,但我建议您添加~/.ssh/config文件并添加类似的内容

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

那么你可以拥有

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

而且您也可以ssh git_hostscp git_host ...一切都会顺利进行。


7

:指定端口时,SSH不使用语法。最简单的方法是编辑您的~/.ssh/config文件并添加:

主机git.host.de
  港口4019

然后仅指定git.host.de一个端口号。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.