git remote add with other SSH port

349,206

Solution 1

You can just do this:

git remote add origin ssh://user@host:1234/srv/git/example

1234 is the ssh port being used

Solution 2

You need to edit your ~/.ssh/config file. Add something like the following:

Host example.com
    Port 1234

A quick google search shows a few different resources that explain it in more detail than me.

Solution 3

Best answer doesn't work for me. I needed ssh:// from the beggining.

# does not work
git remote set-url origin [email protected]:10000/aaa/bbbb/ccc.git
# work
git remote set-url origin ssh://[email protected]:10000/aaa/bbbb/ccc.git

Solution 4

Rather than using the ssh:// protocol prefix, you can continue using the conventional URL form for accessing git over SSH, with one small change. As a reminder, the conventional URL is:

git@host:path/to/repo.git

To specify an alternative port, put brackets around the user@host part, including the port:

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

But if the port change is merely temporary, you can tell git to use a different SSH command instead of changing your repository’s remote URL:

export GIT_SSH_COMMAND='ssh -p port'
git clone git@host:path/to/repo.git # for instance

Solution 5

For those of you editing the ./.git/config

[remote "external"]                                                                                                                                                                                                                                                            
  url = ssh://[email protected]:11720/aaa/bbb/ccc                                                                                                                                                                                                               
  fetch = +refs/heads/*:refs/remotes/external/* 
Share:
349,206
JuanPablo
Author by

JuanPablo

Updated on July 08, 2022

Comments

  • JuanPablo
    JuanPablo almost 2 years

    In Git, how can I add a remote origin server when my host uses a different SSH port?

    git remote add origin ssh://user@host/srv/git/example
    
  • Snicolas
    Snicolas almost 12 years
    Thx. Just a complement : in the path part, use absolute path, not a relative path to user home directory...
  • Hannes
    Hannes almost 12 years
    @Snicolas : Why shall one not use a relative path?
  • Michael van Rooijen
    Michael van Rooijen almost 12 years
    It did work for me. I like this approach better than sticking it in the git remote. Thanks! No need to specify an absolute path either this way.
  • MartinL
    MartinL over 11 years
    @Sincolas It works if you have the repo in the users home directory: /home/someuser/git-repos/example.git --> ssh://someuser@<host>:<port>/~/git-repos/example.git . btw: you get a <name>.git repo by git clone --bare <adress>
  • igorw
    igorw over 11 years
    @Jameo It is an absolute path.
  • kitti
    kitti almost 11 years
    Using gitolite and git 1.8.1.2, I had to use relative paths with this (i.e. ssh://[email protected]:1234/project.git, not ssh://[email protected]:1234/~/repositories/project.git). I'm guessing gitolite does some automagic here.
  • igorw
    igorw almost 11 years
    @RyanP see the other comments.
  • Bijay Rungta
    Bijay Rungta almost 11 years
    Note that it will not work if you remove the protocol. i.e if you try the following, it will not work. git remote add origin user@host:1234/srv/git/example
  • Alexis Wilke
    Alexis Wilke over 10 years
    This works great. Also that way I can have a specific key instead of the default id_rsa. Not only that, my server is picky and more or less you have to have it right quickly enough which fails if you include password. So I use the PasswordAuthentication no as well.
  • MarcH
    MarcH about 10 years
    It's better to have it in the remote than hiding it in the config file like this: when you have everything in just one place you can never forget about the different port number and you can simply copy and paste the URL for anyone else to use.
  • Ragunath Jawahar
    Ragunath Jawahar almost 10 years
    @MarcH It actually depends upon the situation. I like to use random port numbers on on my VPS instances. Having the port inside the config file is one way you can withhold that information from collaborators (That's when you have multiple remotes, the deployment remote host is different from the internal Source Code repo).
  • MarcH
    MarcH almost 10 years
    @RagunathJawahar I don't think using random port numbers is a very common use case.
  • Ragunath Jawahar
    Ragunath Jawahar almost 10 years
    @MarcH Agree, that's why I began with 'It actually depends'.
  • rholmes
    rholmes almost 10 years
    This should work if you get the config file (and other .ssh options and permissions) right. The other advantage is that all other ssh services will get the right port as well (e.g., rsync).
  • Geradlus_RU
    Geradlus_RU about 8 years
    In my case I had to add trailing / to make it work, e.g. ssh://user@host:1234/srv/git/example/ otherwise git failed to reach remote repo
  • kujiy
    kujiy over 7 years
    git remote set-url origin ssh://[email protected]:10000/aaa/bbbb/ccc.git
  • DerWeh
    DerWeh over 7 years
    Using ssh://[email protected]:1234/project.git doesn't work. It always gives fatal: Could not read from remote repository.. However using [email protected]:1234/project.git solves the problem.
  • stamster
    stamster about 7 years
    This is still valid answer, we're using this syntax on Ubuntu 16.04 LTS with git v2.7.4. Thumbs up for the answer, and thumbs down for git not supporting -p or similar port override argument!
  • Apteryx
    Apteryx almost 7 years
    And then in the .git/config file you can simply use host:srv/git/example as the value of the URL (or use git remote add host:/srv/git/example. See man git-pull to learn about the possible URL/host syntaxes.
  • visitantz
    visitantz about 6 years
    Thanks, this works for GitLab Docker container when map to port not 22, spend whole day finnally get things work.
  • m4l490n
    m4l490n almost 6 years
    Perfect. One question though, how do I do this for submodules? It does not seem to work.
  • Thomas Le
    Thomas Le almost 5 years
    Adding the square brackets around the git@host:port worked beautifully for me. I am using gitlab and on that server it requires a non standard port but I also cannot use the absolute path to the repo (I don't know it). Thank you
  • nixlarfs
    nixlarfs over 4 years
    This seems like the most flexible method to me as it supports relative paths and doesn't rely on ssh configs
  • MarcH
    MarcH about 4 years
    For a relative path use Konrad's (better) answer with brackets.
  • coopeu
    coopeu over 3 years
    Debian 10 working in another ssh port. Testing ssh -T [email protected] got Error connection time out. Then this setup git remote set-url origin [email protected]:22/USER/REPOSITORY.git fix the problem. Thanks Kujiy!
  • pratclot
    pratclot over 3 years
    Ehm, it's GIT_SSH_COMMAND, check this out git-scm.com/docs/git
  • raisinrising
    raisinrising over 3 years
    While adding it as a remote using git remote add, I had to wrap the URL in double quotes to have this syntax work. Like so: git remote add gitea "[git@host:port]:path/to/repo.git"
  • Konrad Rudolph
    Konrad Rudolph over 3 years
    @raisinrising That’s unrelated to Git, it depends on the shell you’re using. For sh/Bash/… the quotes are not necessary here.
  • raisinrising
    raisinrising over 3 years
    @Konrad Rudolph Ah that makes sense, thank you. I am running zsh FWIW.
  • xCovelus
    xCovelus over 2 years
    This is the best answer, I think
  • sebasira
    sebasira about 2 years
    Thank you this works great. One little thing, I did not need to add the PORT to the remote URL. Just create the host in ~/.ssh/config and it automatically will use the port specified there