Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Forked from gnarf/..git-pr.md
Last active July 18, 2023 01:28
Show Gist options
  • Save karlhorky/0c454c0c6f894c27911ed5de58d65416 to your computer and use it in GitHub Desktop.
Save karlhorky/0c454c0c6f894c27911ed5de58d65416 to your computer and use it in GitHub Desktop.
Fetch and delete refs to GitHub pull request branches
[alias]
fetch-pr = "!f() { git fetch origin refs/pull/$1/head:pr/$1; } ; f"
delete-prs = "!git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done"

Fetch and delete refs to GitHub pull request branches

GitHub keeps a git references to every pull request apart from the branch. Reference

If you need the commits in a PR again (even with a deleted branch), these git aliases can help.

Copy the aliases to ~/.gitconfig and use as follows:

# Fetch the branch for pull request #24 under a remote tracking branch of pr/24 
git fetch-pr 24
# Checkout the branch
git checkout pr/24
# Delete all local PR branches after you're done
git delete-prs

Also included in karlhorky/dotfiles.


If you need to fetch all GitHub pull requests as local branches use Fetch all GitHub pull requests to local tracking branches

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