411

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
Melebius
  • 6,183
  • 4
  • 39
  • 52
JuanPablo
  • 23,792
  • 39
  • 118
  • 164

9 Answers9

692

You can just do this:

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

1234 is the ssh port being used

minimalpop
  • 6,997
  • 13
  • 68
  • 80
igorw
  • 27,759
  • 5
  • 78
  • 90
  • 19
    Thx. Just a complement : in the path part, use absolute path, not a relative path to user home directory... – Snicolas May 28 '12 at 19:54
  • @Snicolas : Why shall one not use a relative path? – Hannes May 30 '12 at 19:24
  • 15
    @Sincolas It works if you have the repo in the users home directory: **/home/someuser/git-repos/example.git** --> **ssh://someuser@:/~/git-repos/example.git** . btw: you get a _.git_ repo by **git clone --bare ** – MartinL Oct 04 '12 at 18:41
  • @Jameo It *is* an absolute path. – igorw Dec 12 '12 at 21:59
  • Using gitolite and git 1.8.1.2, I had to use relative paths with this (i.e. `ssh://git@myhost.com:1234/project.git`, not `ssh://git@myhost.com:1234/~/repositories/project.git`). I'm guessing gitolite does some automagic here. – kitti Jun 18 '13 at 01:47
  • @RyanP see the other comments. – igorw Jun 18 '13 at 08:59
  • 63
    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` – Bijay Rungta Aug 12 '13 at 08:06
  • 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 – Geradlus_RU Feb 24 '16 at 11:14
  • `git remote set-url origin ssh://user@example.com:10000/aaa/bbbb/ccc.git` – kujiy Nov 25 '16 at 04:03
  • Using `ssh://git@myhost.com:1234/project.git` doesn't work. It always gives `fatal: Could not read from remote repository.`. However using `git@myhost.com:1234/project.git` solves the problem. – DerWeh Nov 25 '16 at 19:56
  • 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! – stamster Apr 28 '17 at 07:19
  • For a relative path use Konrad's (better) answer with brackets. – MarcH May 14 '20 at 04:21
155

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.

bramp
  • 9,581
  • 5
  • 40
  • 46
  • 19
    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. – Michael van Rooijen Jul 21 '12 at 22:32
  • 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. – Alexis Wilke Jan 08 '14 at 01:33
  • 5
    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. – MarcH Mar 30 '14 at 15:42
  • 1
    @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). – Ragunath Jawahar Jun 25 '14 at 12:23
  • @RagunathJawahar I don't think using random port numbers is a very common use case. – MarcH Jun 25 '14 at 13:08
  • @MarcH Agree, that's why I began with 'It actually depends'. – Ragunath Jawahar Jun 25 '14 at 14:06
  • 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). – rholmes Aug 04 '14 at 01:02
  • 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. – Apteryx Aug 01 '17 at 20:55
  • Thanks, this works for GitLab Docker container when map to port not 22, spend whole day finnally get things work. – visitantz Apr 09 '18 at 07:14
  • This is the best answer, I think – xCovelus Feb 10 '22 at 10:04
42

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
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 7
    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 – Thomas Le Jun 22 '19 at 21:26
  • 2
    This seems like the most flexible method to me as it supports relative paths and doesn't rely on ssh configs – nixlarfs Oct 03 '19 at 08:08
  • 1
    Ehm, it's GIT_SSH_COMMAND, check this out https://git-scm.com/docs/git – pratclot Jan 15 '21 at 14:37
  • 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"` – raisinrising Jan 18 '21 at 05:42
  • @raisinrising That’s unrelated to Git, it depends on the shell you’re using. For sh/Bash/… the quotes are *not* necessary here. – Konrad Rudolph Jan 18 '21 at 09:53
  • @Konrad Rudolph Ah that makes sense, thank you. I am running `zsh` FWIW. – raisinrising Jan 18 '21 at 10:14
  • This worked to add a port number to a Git URL that looked like: ```git@server.com:repos/myrepo.git``` in my .git/config file. – Gabriel Hautclocq May 21 '23 at 19:47
41

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

# does not work
git remote set-url origin user@example.com:10000/aaa/bbbb/ccc.git
# work
git remote set-url origin ssh://user@example.com:10000/aaa/bbbb/ccc.git
kujiy
  • 5,833
  • 1
  • 29
  • 35
  • Debian 10 working in another ssh port. Testing ssh -T git@github.com got Error connection time out. Then this setup git remote set-url origin git@github.com:22/USER/REPOSITORY.git fix the problem. Thanks Kujiy! – coopeu Jan 13 '21 at 08:06
19

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

[remote "external"]                                                                                                                                                                                                                                                            
  url = ssh://evanc@www.foo.com:11720/aaa/bbb/ccc                                                                                                                                                                                                               
  fetch = +refs/heads/*:refs/remotes/external/* 
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
7

for gitlab, example ssh port is 2224, therefore:

git remote add ssh://git@192.168.1.100:2224/your_group/your_project.git

nobjta_9x_tq
  • 1,205
  • 14
  • 16
5

Had a similar issue trying to connect to my git server

( have a gitea server in a docker container with ssh-port configured to 2022, instead of standard 22, here as an example my-git-server.lan ).

  1. create ssh key-pair (quiet, without password)
$ ssh-keygen -q -N '' -b 4096 -f ~/.ssh/mykeyfile

(this will create two files: public-key mykeyfile.pub and private-key mykeyfile without any extension)

  1. display contents of the public-key and copy/paste it to your profile's SSH keys in your git-server (similar to how you would do it on Github )
$ cat ~/.ssh/mykeyfile.pub
  1. add following lines to ssh-config to specify git-server's hostname, port and key-file
$ nano ~/.ssh/config
Host my-git-server.lan
  HostName my-git-server.lan
  User git
  Port 2022
  IdentityFile ~/.ssh/mykeyfile

(notice that the username is always git, regardless of your actual username on your git-server)

  1. test ssh connection to your git-server using public-key, .. and receive a success message
$ ssh -T git@my-git-server.lan
Hi there, username! You've successfully authenticated with the key named /Users/username/.ssh/mykeyfile.pub

.. use -v "verbose mode" to analyse any errors:

$ ssh -Tvvv git@my-git-server.lan

(again, notice that the username is always git)

  1. specify your remote address ssh://git@my-git-server.lan:2022/alex/myproject.git for your local git repository (again, notice the user git and the port 2022), .. check remote configuration
$ cd your/local/git/repository/folder
$ git remote add my-git-server ssh://git@my-git-server.lan:2022/alex/myproject.git
$ git remote -v

( here you also see that on my git-server my actual user is alex and repository is myproject )

Done! You can now work with your git-server .. fetch/commit/push etc.

( this is a copy of my post on serverfault.com )


Update: as rightly noted in the comments - you do not necessarily need to specify port 2022 in the remote-url, since it is already configured in ~/.ssh/config file as PORT 2022.

Alex
  • 789
  • 8
  • 8
  • 1
    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 – sebasira May 07 '22 at 02:55
0

Just have a look at how to set up your ~/.ssh/config file correctly.

You can specify different settings for different hosts easily.

To solve your problem you would set

Host github.com 
Port 22 
Host * 
Port 1234

Have a look at the ssh_config manual page, it explains everything you need to know on the first few pages.

Baach
  • 889
  • 10
  • 10
-1

1.git remote add ${shortname} ${url}

2.git remote remove shortname (is remove a remote)

3.git remote -v (is to see your current remote list)

4.git push remote branch

5.git remote rename A B (rename A to B)

6.git remote show shortname (show remote info)

All this works for me.

B.Kingsun
  • 350
  • 3
  • 11