Working With Multiple Git Remotes
Git still stashes most of the world’s active source code, but we’ve branched out beyond storing it all in GitHub. Developers are pushing code to GitLab, Gitea, Forgejo, Codeberg, Gogs, and other alternative hosts. They maintain Git’s lingua franca but defy GitHub’s former hegemony. Some hosts are SaaS-only, while others let you self-host. We have lots of options for source code hosting.
If you work with a lot of repositories, you may find that you also work with a lot of Git hosts. Even on your work machine, if your company has been around long enough to accrue hosting providers or self-hosted installations, you may lose track of which repositories correspond to which remotes. And yeah, I was losing track. To help, I’ve implemented two changes:
- Listing the configured remotes in my Starship prompt.
- Implementing a shell command to open the current repository’s web page in a browser.
These changes have been helpful in my work.
Adding Git Remotes to Starship
Starship offers various configurations for Git, but nothing to include the remotes. It does, however, let you configure custom commands. This is the custom command I added to my starship.toml:
[custom.git_remote]
command = "git remote | while read r; do git remote get-url \"$r\" 2>/dev/null; done | sed 's|.*://||;s|.*@||;s|/.*||;s|:.*||' | sort -u | awk 'NR>1{printf \" | \"}{printf $0} END{print \"\"}'"
when = "git rev-parse --git-dir 2>/dev/null"
style = "#eb763c"
Let’s walk through it, saving the most complicated line, command, for last.
[custom.git_remote]
You name the section custom.<name>, where <name> is an arbitrary name you give your module.
when
This is one of the options to determine whether this custom module runs for your current directory. It runs the command you set, and includes the custom module if that command returns a zero status.
Note: you may be tempted to use detect_folders = ['.git'] to determine whether you’re in a repository directory. OK, so I was tempted, and set it up that way. But that doesn’t work from within a subdirectory inside a Git repository. Better to run a Git command.
git rev-parse is used to “Pick out and massage parameters”. We pass --git-dir; the help for it tells us:
--git-dir
Show $GIT_DIR if defined. Otherwise show the path to the .git directory. The path shown, when
relative, is relative to the current working directory.
If $GIT_DIR is not defined and the current directory is not detected to lie in a Git repository or
work tree print a message to stderr and exit with nonzero status.
Perfect. We run it and redirect stderr to /dev/null
style
This line allows us control the style of the output. We set it to "eb763c", which is one of the oranges in the GitLab logo.
command
This is the command that Starship runs, and outputs the result in your prompt. Here, we’ve strung together a few commands, piping the output of each to the next. Here are the commands:
git remote
while read r; do git remote get-url \"$r\" 2>/dev/null; done
sed 's|.*://||;s|.*@||;s|/.*||;s|:.*||'
sort -u
awk 'NR>1{printf \" | \"}{printf $0} END{print \"\"}'
This string of commands outputs a pipe-delimited list of the unique host names of the Git remotes for the current repository. You’ll usually have only one remote configured, but the command supports multiples.
git remote
Lists the names of the remotes you have configured for the current repository, one per line.
while read r; do git remote get-url \"$r\" 2>/dev/null; done
Loops through the names from the previous command, one at time, and gets the remote URL for them, one per line. Note that we escape the quotation marks around $r because the entire command is inside quotations.
sed 's|.*://||;s|.*@||;s|/.*||;s|:.*||'
Uses sed to extract the host name from the URL. It handles everything I’ve thrown at it – HTTPS, GIT, SSH. It will take something like git@github.com:hoop33/dotfiles.git and return github.com.
sort -u
Sorts the resulting host names alphabetically and removes duplicates.
awk 'NR>1{printf \" | \"}{printf $0} END{print \"\"}'
Uses awk to print the host names, separating each with |.
Examples
You should see something like this at the end of your prompt for a single configured remote:

Two remotes will look something like this:

Opening a Remote’s Web Page from the Terminal
Beyond knowing the host, I wanted to quickly open a repository’s web page — maybe to check on the status of a Merge Request, say, or a CI/CD pipeline. I found a git open implementation on GitHub (maybe it was this one — it’s been awhile), and it didn’t work with our self-hosted installations. I don’t remember why. I just searched and found a few more git open options, including this one from a month ago. I’ve been happy with this shell/zsh function I coauthored with Claude. It works on macOS, Linux, and (probably) Windows. It handles multiple remotes for a single repository, using fzf to let you choose which. I’m not going to break this down line-by-line, but generally this function:
- Reads the configured remotes.
- If only one configured remote exists, uses it, else uses
fzfto present a selection menu. - Reads the URL for the selected remote.
- Converts the URL to the corresponding web page.
- Figures out the command to open your browser, based on your operating system.
- Opens the URL from #4 using the command from #5.
# Open the web page for the git repo in cwd
function gopen() {
local remote_name remote url
local -a remotes
# gather configured remotes; single remote auto-picked, 2+ prompt via fzf
remotes=("${(@f)$(git remote 2>/dev/null)}") || { echo "error: not a git repository"; return 1; }
remotes=("${remotes[@]:#}")
if (( ${#remotes[@]} == 0 )); then
echo "error: no remotes configured"; return 1
elif (( ${#remotes[@]} == 1 )); then
remote_name="${remotes[1]}"
else
remote_name="$(printf '%s\n' "${remotes[@]}" | fzf --prompt='remote> ')" || return 1
[[ -z "$remote_name" ]] && return 1
fi
# resolve chosen remote name to its actual url
remote="$(git remote get-url "$remote_name" 2>/dev/null)" || { echo "error: could not resolve remote $remote_name"; return 1; }
# normalize remote url (scp-style, ssh://, https://) to a browsable https url
if [[ "$remote" == git@* ]]; then
url="${remote#git@}"
url="https://${url/://}"
url="${url%.git}"
elif [[ "$remote" == ssh://* ]]; then
url="${remote#ssh://git@}"
url="${url%.git}"
local host_part="${url%%/*}"
local path_part="${url#*/}"
host_part="${host_part%%:*}"
url="https://${host_part}/${path_part}"
elif [[ "$remote" == https://* ]]; then
url="${remote%.git}"
else
echo "error: unsupported remote format: $remote"; return 1
fi
# pick platform url opener
local opener
if command -v open >/dev/null; then
opener=open
elif command -v xdg-open >/dev/null; then
opener=xdg-open
elif command -v wslview >/dev/null; then
opener=wslview
else
echo "error: no url opener found"; return 1
fi
"$opener" "$url"
}
Paste that command into your dot files, reload your shell configuration, and in your terminal type gopen. Voila.
Conclusion
Working with multiple Git remotes can be painful. Take these steps to reduce that pain and streamline your workflow.