373
votes

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
7

7 Answers

626
votes

You can just do this:

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

1234 is the ssh port being used

141
votes

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.

35
votes

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
25
votes

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
18
votes

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

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

for gitlab, example ssh port is 2224, therefore:

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

-1
votes

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.