My Git helper shortcuts
I’m a hardcore GitHub user, as most of my projects are hosted there. I usually use gh repo view --web to open the current codebase’s GitHub repository from a terminal. I don’t use desktop apps such as SourceTree or GitHub Desktop.
Recently, however, I started using GitLab for some experimental projects. At work, we use Azure DevOps for our code repositories. Azure DevOps, for example, already has a similar command for opening a remote repository, but you need to specify its repository name, so it isn’t particularly convenient.
What if I told you that we don’t even need the az or gh commands? Git can already give us the remote repository URL using:
git remote get-url originIt works regardless of which remote repository provider we’re using! So, I created a PowerShell function that wraps the command neatly. Oh, and I also included a function for viewing commit details in the remote repository. This lets us see more context about the commit, such as the associated PR, user story, etc.
PowerShell
On Windows, you can open the PowerShell profile.
code $PROFILEThen, paste the following function.
# or stands for openrepofunction or { $url = git config --get remote.origin.url
# Convert SSH URL to HTTPS if needed if ($url -match "^git@(.+):(.+)$") { $url = "https://$($matches[1])/$($matches[2] -replace '\.git$','')" } elseif ($url -match "^https://") { $url = $url -replace '\.git$','' }
Write-Host "Opening $url in browser..." Start-Process $url}
# oc stands for opencommitfunction oc { param ( [string]$commitHash )
if (-not $commitHash) { throw "Commit hash is required. Usage: oc <commit-hash>" }
$url = git config --get remote.origin.url
# Convert SSH URL to HTTPS if needed if ($url -match "^git@(.+):(.+)$") { $url = "https://$($matches[1])/$($matches[2] -replace '\.git$','')" } elseif ($url -match "^https://") { $url = $url -replace '\.git$','' }
$commitUrl = "$url/commit/$commitHash" Write-Host "Opening $commitUrl in browser..." Start-Process $commitUrl}Open a new PowerShell session or run the command below to load the functions:
. $PROFILEZSH
On macos, you can add the following code to your ~/.zshrc file.
code ~/.zshrcThen, paste this.
# git helpers_repo_web_url() { local remote_name remote_url host repo_path
# Prefer origin; otherwise fall back to the first configured remote. remote_name="${1:-origin}" remote_url="$(git config --get "remote.${remote_name}.url")" if [[ -z "$remote_url" ]]; then remote_name="$(git remote | head -n 1)" remote_url="$(git config --get "remote.${remote_name}.url")" fi
if [[ -z "$remote_url" ]]; then echo "Not a git repo or no remotes configured." return 1 fi
# Azure DevOps SSH format: git@ssh.dev.azure.com:v3/org/project/repo if [[ "$remote_url" =~ '^git@ssh\.dev\.azure\.com:v3/(.+)/(.+)/(.+)$' ]]; then echo "https://dev.azure.com/${match[1]}/${match[2]}/_git/${match[3]}" return 0 fi
# SCP-like SSH format: git@host:owner/repo(.git) if [[ "$remote_url" =~ '^git@([^:]+):(.+)$' ]]; then host="${match[1]}" repo_path="${match[2]}" repo_path="${repo_path%.git}" echo "https://${host}/${repo_path}" return 0 fi
# SSH URL format: ssh://git@host/owner/repo(.git) if [[ "$remote_url" =~ '^ssh://([^@]+@)?([^/]+)/(.+)$' ]]; then host="${match[2]}" repo_path="${match[3]}" repo_path="${repo_path%.git}" echo "https://${host}/${repo_path}" return 0 fi
# HTTPS URL (strip optional auth user and trailing .git) if [[ "$remote_url" =~ '^https://([^/]+)/(.+)$' ]]; then host="${match[1]}" repo_path="${match[2]}" host="${host#*@}" repo_path="${repo_path%.git}" echo "https://${host}/${repo_path}" return 0 fi
echo "Unsupported remote URL format: ${remote_url}" return 1}
# or = openrepoor() { local url url="$(_repo_web_url)" || return 1 echo "Opening ${url} in browser..." open "$url"}
# oc = open commitoc() { local commit_hash base_url commit_url commit_hash="$1"
if [[ -z "$commit_hash" ]]; then echo "Commit hash is required. Usage: oc <commit-hash>" return 1 fi
base_url="$(_repo_web_url)" || return 1
# Provider-specific commit routes. if [[ "$base_url" == *"gitlab"* ]]; then commit_url="${base_url}/-/commit/${commit_hash}" elif [[ "$base_url" == *"bitbucket"* ]]; then commit_url="${base_url}/commits/${commit_hash}" else commit_url="${base_url}/commit/${commit_hash}" fi
echo "Opening ${commit_url} in browser..." open "$commit_url"}Demo
I’ve tested the functions on Windows and macos with multiple Git providers, including GitHub, GitLab, and Azure DevOps. The functions should work with any Git provider.
The demos below were recorded on macos.