Skip to content

Instantly share code, notes, and snippets.

@ravshansbox
Created June 7, 2026 15:04
Show Gist options
  • Select an option

  • Save ravshansbox/e76ddfd1f79d55bf5156be424e37d4ae to your computer and use it in GitHub Desktop.

Select an option

Save ravshansbox/e76ddfd1f79d55bf5156be424e37d4ae to your computer and use it in GitHub Desktop.
After cloning a GitHub fork, set 'upstream' remote to the original parent repo. Tiny, no gh repo set-default needed.
#!/usr/bin/env bash
set -euo pipefail
slug=$(git remote get-url origin | sed -E 's#^.*[/:]([^/]+/[^/.]+)(\.git)?$#\1#')
url=$(gh api "repos/$slug" --template '{{if .fork}}{{.parent.ssh_url}}{{end}}' 2>/dev/null || true)
[[ -z $url ]] && { echo "not a fork"; exit 0; }
git remote add upstream "$url" 2>/dev/null || git remote set-url upstream "$url"
git fetch upstream --prune
echo "upstream → $url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment