Git
SSH
Remote
Port
DevOps

Git remote add with a custom SSH port

Codemia Knowledge Hub ·

Use a full ssh:// URL with the port in it, or define a host alias with the port in ~/.ssh/config. Git's short scp-style syntax (user@host:path) has no place for a port, which is where most of the confusion comes from.

Put the port in an ssh:// URL

bash
git remote add origin ssh://[email protected]:2222/home/git/project.git

This connects as user git to example.com on port 2222 and uses /home/git/project.git as the repository path. Confirm the remote with:

bash
git remote -v

This form is unambiguous and works well when you know the exact host, user, port and path.

Why the short syntax cannot take a port

Git also accepts the shorter scp-style syntax:

bash
[email protected]:project.git

Here the colon already separates the host from the remote path. So [email protected]:2222/project.git does not mean "port 2222"; Git treats 2222/project.git as the path on the server.

Use an SSH config alias for repeated access

If you connect to the same host often, an alias is cleaner than repeating the port in every remote. Add this to ~/.ssh/config:

sshconfig
1Host git-alt
2    HostName example.com
3    User git
4    Port 2222

Then use the alias in Git:

bash
git remote add origin git-alt:/home/git/project.git

The repository configuration stays short, and the connection details live in the SSH layer, where they also apply to plain ssh, scp and other tools.

Test SSH before blaming Git

Check that SSH itself reaches the server on the custom port:

bash
ssh -p 2222 [email protected]

Or with the alias:

bash
ssh git-alt

If that fails, the problem is not git remote add. It is usually authentication, firewall access, host keys or the server path.

Changing the port on an existing remote

If the remote exists and only the port is wrong, update the URL instead of deleting and re-adding it:

bash
git remote set-url origin ssh://[email protected]:2222/home/git/project.git

This keeps the remote name and avoids churn in scripts or documentation.

Repository paths differ between servers

Even with the right port, servers expect different repository roots, such as /home/git/project.git, /srv/git/project.git or a hosting-specific namespace. If SSH connects but Git cannot find the repository, check the path.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design