git remote add with other SSH port
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/*
JuanPablo
Updated on July 08, 2022Comments
-
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 almost 12 yearsThx. Just a complement : in the path part, use absolute path, not a relative path to user home directory...
-
Hannes almost 12 years@Snicolas : Why shall one not use a relative path?
-
Michael van Rooijen almost 12 yearsIt 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 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 over 11 years@Jameo It is an absolute path.
-
kitti almost 11 yearsUsing gitolite and git 1.8.1.2, I had to use relative paths with this (i.e.
ssh://[email protected]:1234/project.git
, notssh://[email protected]:1234/~/repositories/project.git
). I'm guessing gitolite does some automagic here. -
igorw almost 11 years@RyanP see the other comments.
-
Bijay Rungta almost 11 yearsNote 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 over 10 yearsThis 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 about 10 yearsIt'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 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 almost 10 years@RagunathJawahar I don't think using random port numbers is a very common use case.
-
Ragunath Jawahar almost 10 years@MarcH Agree, that's why I began with 'It actually depends'.
-
rholmes almost 10 yearsThis 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 about 8 yearsIn 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 over 7 years
git remote set-url origin ssh://[email protected]:10000/aaa/bbbb/ccc.git
-
DerWeh over 7 yearsUsing
ssh://[email protected]:1234/project.git
doesn't work. It always givesfatal: Could not read from remote repository.
. However using[email protected]:1234/project.git
solves the problem. -
stamster about 7 yearsThis 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 almost 7 yearsAnd then in the .git/config file you can simply use
host:srv/git/example
as the value of the URL (or usegit remote add host:/srv/git/example
. Seeman git-pull
to learn about the possible URL/host syntaxes. -
visitantz about 6 yearsThanks, this works for GitLab Docker container when map to port not 22, spend whole day finnally get things work.
-
m4l490n almost 6 yearsPerfect. One question though, how do I do this for submodules? It does not seem to work.
-
Thomas Le almost 5 yearsAdding 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 over 4 yearsThis seems like the most flexible method to me as it supports relative paths and doesn't rely on ssh configs
-
MarcH about 4 yearsFor a relative path use Konrad's (better) answer with brackets.
-
coopeu over 3 yearsDebian 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 over 3 yearsEhm, it's GIT_SSH_COMMAND, check this out git-scm.com/docs/git
-
raisinrising over 3 yearsWhile 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 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 over 3 years@Konrad Rudolph Ah that makes sense, thank you. I am running
zsh
FWIW. -
xCovelus over 2 yearsThis is the best answer, I think
-
sebasira about 2 yearsThank 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