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
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:
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:
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:
Then use the alias in 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:
Or with the alias:
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:
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.
Related reading
- GitHub Actions build outside vs inside container?
- Good examples using java.util.logging
- Google cloud Kubernetes deployment error Field is immutable
- Graceful shutdown of golang web server
- Git remote branch deleted, but still it appears in ''branch -a''
- Git replacing LF with CRLF
- Hardware requirement for apache kafka
- Hazelcast SlowOperationDetector to identify operations with less than 1 sec execution time

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.