From 3175db49002f4e626656e79cdd689b127eaff3f8 Mon Sep 17 00:00:00 2001 From: Uyanide Date: Tue, 18 Nov 2025 23:03:50 +0100 Subject: [PATCH] add a memo about git --- README.md | 2 +- ...y-machine.png => works-on-my-machines.png} | Bin memo/git-remote.md | 108 ++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) rename assets/{works-on-my-machine.png => works-on-my-machines.png} (100%) create mode 100644 memo/git-remote.md diff --git a/README.md b/README.md index 06685dd..9812c0a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Works on my machine(s) +Works on my machine(s) ## How it looks like... diff --git a/assets/works-on-my-machine.png b/assets/works-on-my-machines.png similarity index 100% rename from assets/works-on-my-machine.png rename to assets/works-on-my-machines.png diff --git a/memo/git-remote.md b/memo/git-remote.md new file mode 100644 index 0000000..a7ea14a --- /dev/null +++ b/memo/git-remote.md @@ -0,0 +1,108 @@ +## 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: + +```sh +git remote add origin gitea:username/repo.git +``` + +2. 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. + +```sh +git remote add origin ssh://git@example.tld:222/username/repo.git +``` + +3. Use `GIT_SSH_COMMAND` environment variable + +```sh +GIT_SSH_COMMAND='ssh -p 222' +``` + +4. and more... + - aliases + - scripts + - etc. + +## Local Repo with Multiple Remotes + +> [!TIP] +> +> Check current remotes with: +> +> ```sh +> git remote -v +> ``` +> +> and remove a remote with: +> +> ```sh +> git remote remove +> ``` + +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 + +```sh +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: + +```sh +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. + +2. Use `git remote set-url --add` + +```sh +git remote set-url --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: + +```sh +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.