git remote add with other SSH port

GitSshPort

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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

Solution 4 - Git

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 - Git

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

[remote "external"]                                                                                                                                                                                                                                                            
  url = ssh://[email protected]:11720/aaa/bbb/ccc                                                                                                                                                                                                               
  fetch = +refs/heads/*:refs/remotes/external/* 

Solution 6 - Git

for gitlab, example ssh port is 2224, therefore:

> git remote add ssh://[email protected]:2224/your_group/your_project.git

Solution 7 - Git

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://[email protected]: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://[email protected]: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.

Solution 8 - Git

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.

Solution 9 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuanPabloView Question on Stackoverflow
Solution 1 - GitigorwView Answer on Stackoverflow
Solution 2 - GitbrampView Answer on Stackoverflow
Solution 3 - GitkujiyView Answer on Stackoverflow
Solution 4 - GitKonrad RudolphView Answer on Stackoverflow
Solution 5 - GitEvan CarrollView Answer on Stackoverflow
Solution 6 - Gitnobjta_9x_tqView Answer on Stackoverflow
Solution 7 - GitAlexView Answer on Stackoverflow
Solution 8 - GitBaachView Answer on Stackoverflow
Solution 9 - GitWhu_KingsunView Answer on Stackoverflow