Files
dotfiles/memo/git-remote.md

2.6 KiB

Gitea with custom SSH port

Since in most cases Gitea will be deployed within containers, it cannot use the same port (22) as the host machine to handle git operations via SSH. In my case the port 222 is used instead. Meanwhiles, this can cause some extra steps to set the correct remote URL for local repositories, since the port is different from the default one.

There are few approaches to handle this:

  1. Edit ~/.ssh/config

like:

Host gitea
    HostName git.example.tld
    User git
    Port 222
    IdentityFile /path/to/private/key

then:

git remote add origin gitea:username/repo.git
  1. Use full URL with port

Warning

The scp-like syntax user@host:repo.git does not support port specification. ssh:// URL scheme must be used instead.

git remote add origin ssh://git@example.tld:222/username/repo.git
  1. Use GIT_SSH_COMMAND environment variable
GIT_SSH_COMMAND='ssh -p 222'
  1. and more...
    • aliases
    • scripts
    • etc.

Local Repo with Multiple Remotes

Tip

Check current remotes with:

git remote -v

and remove a remote with:

git remote remove <name>

Local repositories can have multiple remotes configured. This is useful when pushing to the primary repository and also to a backup repository.

And there are multiple ways to add multiple remotes:

  1. Use different names for remotes
git remote add origin ssh://git@example.tld/username/repo.git

git remote add backup ssh://git@backup.tld/username/repo.git

then push to both remotes:

git push origin main
git push backup main

Without referring to remotes explicitly, git will use origin by default. To fetch from all remotes, use git fetch --all or directly git pull --all.

Note

The command git push --all is for pushing all local branches, not for pushing to all configured remote URLs.

  1. Use git remote set-url --add
git remote add origin ssh://git@example.tld/username/repo.git
git remote set-url --add origin ssh://git@backup.tld/username/repo.git

then push to both remotes:

git push origin

A single git push origin will push to all URLs configured for the origin remote.

Important

By default, git fetch and git pull will only interact with the first (fetch) URL configured for a remote.

The git fetch --all command fetches from all configured remotes (e.g., origin, backup), not from all URLs within a single remote.

Similarly, git pull --all will first run git fetch --all and then merge the current local branch with its upstream branch. The merge action only applies to the current branch.