6

I have a git (version 2.1.2) repository with an ssh remote:

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

Which fails to 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.

Unless… I use the --verbose switch:

$ 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'

I augmented the ssh log level as hinted in that answer, but the output for git push (without --verbose) was the exact same.

Where could it come from?

As nwinkler suggested, here is the output of the two commands with 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':

So unless I use --verbose, there indeed is an extra '-p' option with an empty argument.

EDIT: this is getting more obscure:

$ 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.
Community
  • 1
  • 1
Dettorer
  • 1,247
  • 1
  • 8
  • 26
  • Do you have anything in your `.ssh/config` file? What happens if you run `ssh -vvv dettorer@dettorer.net`? – nwinkler Dec 11 '14 at 12:35
  • @nwinkler, nothing in my `.ssh/config` (apart from when I augmented the ssh's log level). Nothing wrong happens when using `ssh -vvv` (and I can ssh correctly even without `-vvv`). – Dettorer Dec 11 '14 at 12:41
  • Also, what's the output of running `GIT_TRACE=2 git push`? It should print the exact `ssh` command that's being run. – nwinkler Dec 11 '14 at 12:41
  • The _Bad Port ''_ message looks like there's an extra `-p ''` argument to the _ssh_ command somewhere, trying to set the port for ssh. – nwinkler Dec 11 '14 at 12:43
  • Indeed there is, I'm editing my question to show the output. – Dettorer Dec 11 '14 at 12:43
  • This is getting darker, adding a random remote (even invalid) fixes it (I edited to show this). I'll test with the last `git` version – Dettorer Dec 11 '14 at 13:00
  • Check whether you have anything suspicious in your environment variables, aliases, or Git configuration. – nwinkler Dec 11 '14 at 13:07
  • 1
    This now fixed with Git 2.3.7 (April 2015). See [my answer below](http://stackoverflow.com/a/29912423/6309) – VonC Apr 28 '15 at 07:00

2 Answers2

9

OK, after seeing your comments, I'm pretty sure I know what's wrong.

Can you try to change your remote URL to this:

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

You've got an extra colon there, which seems to be causing the issue of the additional port. I don't know why running it with -v fixes the problem.

The git help shows that the following is the supported format for the ssh protocol:

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

As you can see, the colon is only required if you need to set a port. Leave it off if you want to use the standard port.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 1
    That's what I was beginning to think, I'll see if I can reproduce with a dummy repository and the last git version and possibly file a bug report (for the different behavior induced by `--verbose`). It works without the extra '`:`', thanks! – Dettorer Dec 11 '14 at 13:27
  • You're welcome! +1 on the bug report, if this turns out to be an issue in Git. – nwinkler Dec 11 '14 at 13:29
  • Come to think of it - it would be great if Git was smart enough to notify you of the mistake. It shouldn't try to add a `-p` argument to ssh when you didn't define a port. A message or hint of some kind would have been nice. – nwinkler Dec 11 '14 at 13:40
  • 2
    It even works fine `-v` and ignore the empty port. It may be that a different url parser is used when in verbose mode. – Dettorer Dec 11 '14 at 13:42
2

Note: with Git 2.3.7 (released yesterday, April, 27 2015), this error "Bad port ''" is no more.

See commit ad34ad6, from commit 6b6c5f7 (by Torsten Bögershausen tboegi)

connect.c: ignore extra colon after hostname

Ignore an extra ':' at the end of the hostname in URL's like "ssh://example.com:/path/to/repo"

The colon is meant to separate a port number from the hostname.
If the port is empty, the colon should be ignored, see RFC 3986.

It had been working for URLs with ssh:// scheme, but was unintentionally broken in 86ceb3, "allow ssh://user@[2001:db8::1]/repo.git" (Git 2.3.4).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250