Git and libgit2

Git URLs for SSH

Published 2025-01-15. Last modified 2025-01-17.
Time to read: 3 minutes.

This page is part of the git collection.

Git URLs that rely upon SSH can be specified in various ways. The following three examples accomplish exactly the same thing.

You can use the scp-like SSH syntax. The scp-like syntax assumes that relative paths are based from the user home directory. My home directory on server gojira is /home/mslinn.

Relative scp-like Syntax
$ git clone gojira:bare_repo1
Absolute scp-like Syntax
$ git clone gojira:/home/mslinn/bare_repo1

You can also use the standard SSH URI syntax for Git URLs. The SSH Git syntax requires an absolute path.

SSH URI Syntax
$ ssh://gojira/home/mslinn/bare_repo1

Do Not Mention Port 22

SSH runs on port 22 by default, so there is no need to specify port 22 in a URL or URI. Doing so only makes the URL more complicated.

Do not specify the default port (22)
ssh://git@git.my_domain.com:22/path/to/my/repo

Avoid This Common Problem

If you use the URL syntax, the port separator is a slash, not a colon.

This example, which does not work, has two colons in it. As well, this example specifies the default port for ssh (22), which unnecessarily complicates the URL.

This is the same problem again
ssh://git@git.myurl.de:22/mnt/raid/git-repos/reponame/info/lfs
This works, but is unnecessarily complex
ssh://git@git.myurl.de/22/mnt/raid/git-repos/reponame/info/lfs

Historical Background

The explanation of these two formats is historical:

All three of the remote operation programs in OpenSSH (ssh, scp, sftp), accept arguments that comply to the SSH URI scheme, described in its own IETF specification.

Broadly speaking, any URI is composed of five components: scheme, authority, path, query, and fragment. Furthermore, the authority can be composed of user info, host, and port. In the case of the SSH URI scheme, only scheme, authority, and path are allowed.

scheme     authority           path
  |           |                 |
/‾‾\/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\
ssh://git@github.com:22/myuser/myrepo.git
      \_/\_________/\_/
       |      |      |
      user   host   port

However, there's an alternative syntax that's quite similar to the standard SSH URI scheme, but not exactly compliant with it. This syntax is a heritage from the old rcp (remote copy) program created in 1981, almost a decade and a half before the creation of the URI protocol. When the Berkeley r-commands were released, to specify the destination of an operation in rsh (remote shell) or rlogin (remote login) it was only required to specify the remote machine in which the operation was going to be performed, and probably the user in that machine as well. At that time, the consensus was the syntax username@hostname.

The rcp program, however, had an additional condition: to copy files from, or to, one remote location, the developer was required to specify the path of the file in that remote machine, in addition to the user and host, just as the cp program does. The syntax they devised was to append the path to the end of the user/host declaration, separated by a colon: username@hostname:path/to/file (as described in its own man page).

git@github.com:myuser/myrepo.git
\_/ \________/ \_______________/
 |      |              |
user   host           path

When the OpenSSH team implemented the scp program, they wanted to provide a familiar API to the developers already using rcp, so they decided to keep support for this old, custom syntax, in addition to the new standardized SSH URI scheme. Then, in 2005, Git entered the scene with native support for SSH, probably using OpenSSH under the hood, and the rcp-like syntax ended up being used when SSH is used for remote operations like clone, fetch, push, and pull.

The Git documentation calls this syntax scp-like syntax, and it is briefly described in the scp man page, the git-clone docs, and the Git protocols docs. In the end, despite its lack of standardization, it seems to be the syntax used by all cloud Git services like GitHub or Bitbucket, when offering a URL for cloning repos.

 – From StackOverflow
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.