Skip to content

Instantly share code, notes, and snippets.

@joaomoreno
Forked from gnarf/..git-pr.md
Last active June 4, 2021 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaomoreno/23a9c2934dfa9f5bfad79455b7469c87 to your computer and use it in GitHub Desktop.
Save joaomoreno/23a9c2934dfa9f5bfad79455b7469c87 to your computer and use it in GitHub Desktop.
git pr - Global .gitconfig aliases for Pull Request Managment

Install

Either copy the aliases from the .gitconfig or run the commands in add-pr-alias.sh

Usage

Easily checkout local copies of pull requests from remotes:

  • git pr 4 - creates local branch pr/4 from the github upstream(if it exists) or origin remote and merges main into it
  • git pr 4 someremote - creates local branch pr/4 from someremote remote and checks it out
  • git pr-merge - merges the currently checked out PR into main
  • git pr-clean - removes all pr/* branches from your local repo
# for github remotes
[alias]
pr = "!f() { git fetch ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head && git checkout -b pr/$1 FETCH_HEAD && git merge main; }; f"
pr-clean = "!git for-each-ref refs/heads/pr/* --format=\"%(refname)\" | while read ref ; do branch=${ref#refs/heads/} ; git branch -d $branch ; done"
pr-merge = "!f() { PR=$(git symbolic-ref HEAD) && (case $PR in \"refs/heads/pr/\"*) git checkout main && git merge $PR;; *) echo \"HEAD is not a PR branch: $PR\";; esac); }; f"
@gnarf
Copy link

gnarf commented Jun 5, 2018

github has a refs/pull/*/merge also that you could probs just fetch - not sure if you looked into that.

@gnarf
Copy link

gnarf commented Jun 5, 2018

I'd consider adding the change to the original gist if the process works, the thing I'd be worried about in this setup is if you git pr from a local branch, it doesn't make sure that the base is on latest master.

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