Skip to content

Instantly share code, notes, and snippets.

@HarHarLinks
Created February 23, 2026 06:52
Show Gist options
  • Select an option

  • Save HarHarLinks/8f86c51c460efbcf9d32adeb789d0587 to your computer and use it in GitHub Desktop.

Select an option

Save HarHarLinks/8f86c51c460efbcf9d32adeb789d0587 to your computer and use it in GitHub Desktop.
Some functions for .bashrc/.zshrc to make working with PRs from forks easier
gco-pr(){
git fetch origin pull/$1/head:$1 && git checkout $1
}
gp-pr(){
branch="$(git branch --show-current)"
remote_organisation="$(echo "$1" | sed 's/:.*//')"
remote_branch="$(echo "$1" | sed 's/.*://')"
origin="$(git remote get-url origin)"
if [[ "$origin" == "git@"* ]]; then
# parse ssh remote
host="$(echo "$origin" | sed -E 's/git@([^:]+):([^\/]+)\/(.+)\.git/\1/')"
organisation="$(echo "$origin" | sed -E 's/git@([^:]+):([^\/]+)\/(.+)\.git/\2/')"
repo="$(echo "$origin" | sed -E 's/git@([^:]+):([^\/]+)\/(.+)\.git/\3/')"
echo git push "git@$host:$remote_organisation/$repo.git" "$branch:$remote_branch"
git push "git@$host:$remote_organisation/$repo.git" "$branch:$remote_branch"
else
# parse https remote
host="$(echo "$origin" | sed -E 's/https:\/\/([^/]+)\/([^\/]+)\/(.+)\.git/\1/')"
organisation="$(echo "$origin" | sed -E 's/https:\/\/([^/]+)\/([^\/]+)\/(.+)\.git/\2/')"
repo="$(echo "$origin" | sed -E 's/https:\/\/([^/]+)\/([^\/]+)\/(.+)\.git/\3/')"
echo git push "https://$host/$remote_organisation/$repo.git" "$branch:$remote_branch"
git push "https://$host/$remote_organisation/$repo.git" "$branch:$remote_branch"
fi
}
@HarHarLinks
Copy link
Author

I should probably mention that gco-pr takes the PR number as argument and gp-pr takes the remote user and branch separated by a colon which is what you get when you click the copy button next to the source branch name when viewing a GitHub PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment