使用SSH远程推送Git失败,除非使用详细模式

6

我有一个版本为2.1.2git代码库,其中包含一个ssh的远程仓库:

$ git remote -v
origin  ssh://dettorer@dettorer.net:/home/dettorer/my_project (fetch)
origin  ssh://dettorer@dettorer.net:/home/dettorer/my_project (push)

哪些推送失败:

$ git push
Bad port ''
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

除非我使用--verbose开关:
$ git push --verbose
Pushing to ssh://dettorer@dettorer.net:/home/dettorer/my_project
Enter passphrase for key '/home/dettorer/.ssh/id_rsa': 
Counting objects: 7, done.
...
To ssh://dettorer@dettorer.net:/home/dettorer/my_project
   e633fe9..5d2e9de  master -> master
updating local tracking ref 'refs/remotes/origin/master'

我按照这个答案的提示,增加了 ssh 的日志级别,但是在没有使用 --verbose 的情况下,git push 的输出仍然完全相同。

它可能来自哪里呢?

正如 nwinkler 建议的那样,以下是两个带有 GIT_TRACE=2 的命令的输出:

$ GIT_TRACE=2 git push
13:42:33.002392 git.c:349               trace: built-in: git 'push'
13:42:33.033594 run-command.c:341       trace: run_command: 'ssh' '-p' '' 'dettorer@dettorer.net' 'git-receive-pack '\''/home/dettorer/my_project'\'''
Bad port ''
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

$ GIT_TRACE=2 git push -v
13:42:39.929236 git.c:349               trace: built-in: git 'push' '-v'
Pushing to ssh://dettorer@dettorer.net:/home/dettorer/my_project
13:42:39.944837 run-command.c:341       trace: run_command: 'ssh' 'dettorer@dettorer.net' 'git-receive-pack '\''/home/dettorer/my_project'\'''
Enter passphrase for key '/home/dettorer/.ssh/id_rsa':

除非我使用--verbose,否则确实有一个带有空参数的额外'-p'选项。
编辑:这变得更加晦涩了:
$ git push origin
Bad port ''
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

$ git remote add test test
$ git push origin
Enter passphrase for key '/home/dettorer/.ssh/id_rsa': 

$ git remote remove test
$ git push origin
Bad port ''
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

你的.ssh/config文件里有什么内容吗?如果你运行ssh -vvv dettorer@dettorer.net会发生什么? - nwinkler
@nwinkler,我的.ssh/config里什么也没有(除了我增加ssh的日志级别时)。当使用ssh -vvv时没有任何问题(即使没有-vvv,我也可以正确地进行ssh)。 - Dettorer
另外,运行 GIT_TRACE=2 git push 的输出是什么?它应该打印出正在运行的确切 ssh 命令。 - nwinkler
确实有,我正在编辑我的问题来展示输出。 - Dettorer
1
这个问题现在已经在Git 2.3.7(2015年4月)中得到了解决。请参见我的回答 - VonC
显示剩余3条评论
2个回答

9

好的,在看了您的评论之后,我很确定知道问题出在哪里了。

您可以尝试将远程URL更改为以下内容:

ssh://dettorer@dettorer.net/home/dettorer/my_project

您多输入了一个冒号,这似乎导致了额外端口的问题。我不知道为什么使用-v选项会修复该问题。

git帮助显示ssh协议支持以下格式:

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

如您所见,只有需要设置端口时才需要使用冒号。如果要使用标准端口,请省略冒号。


1
这正是我开始思考的,我会尝试使用虚拟存储库和最新的git版本来重现,并可能提交错误报告(针对--verbose引起的不同行为)。没有额外的“:`”,它可以正常工作,谢谢! - Dettorer
不客气!如果这个问题在Git中成为一个问题,那么在错误报告上+1。 - nwinkler
说起来,如果Git足够聪明,能够通知你错误的话那就太好了。当你没有定义端口时,它不应该尝试向ssh添加“-p”参数。一条消息或某种提示会很好。 - nwinkler
2
即使使用“-v”参数并忽略空端口,它也能正常工作。当处于详细模式时,可能会使用不同的URL解析器。 - Dettorer

2
注意:自Git 2.3.7(于2015年4月27日发布)起,此错误“Bad port''”已不再存在。
请参见提交ad34ad6,源自提交6b6c5f7(由Torsten Bögershausen tboegi提供)。

connect.c:忽略主机名后面多余的冒号

对于URL中类似于"ssh://example.com:/path/to/repo"的情况,请忽略主机名末尾的额外':'。

冒号用于将端口号与主机名分开。
如果端口为空,则应忽略冒号,参见RFC 3986。

它在ssh://方案的URL中一直有效,但在86ceb3,“允许ssh://user@[2001:db8::1]/repo.git(Git 2.3.4)中无意中被破坏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接