Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active August 29, 2015 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/cdaa5555b3578e3763f5 to your computer and use it in GitHub Desktop.
Save ericboehs/cdaa5555b3578e3763f5 to your computer and use it in GitHub Desktop.
Merging github pull requests by ID
# These two aliases will allow you to check out the BRANCH that a PR is
# associated with by PR ID (gcopr) and also merge a pull request by ID into
# master (gmpr).
#
# I use this in conjunction with the ghi gem to allow me to review and merge pull
# requests from the command line. To show open pull requests (I alias this to `prs`):
#
# ghi list | grep --color=auto '↑' --color=none
# Git checkout pull request by ID
gcopr() {
git branch -D pr-$1 2>&1 | grep -v 'not found.'
git fetch origin || kill -INT $$
git checkout master 2>&1 | grep -v "Already on 'master'" || kill -INT $$
git fetch origin pull/$1/head:pr-$1 || kill -INT $$
BRANCH="$(git branch -a --contains pr-$1 | grep -v pr-$1 | tr -d '[:space:]' | cut -f3 -d/)"
git branch -D $BRANCH 2>&1 | grep -v 'not found.'
git branch -D pr-$1 1>/dev/null
git checkout $BRANCH
}
# Git merge pull request
gmpr() {
gcopr $1
git checkout master
git merge --no-ff -m "Merge pull request #$1 from $BRANCH" $BRANCH && (
git push
git branch -D $BRANCH
git push --delete origin $BRANCH
git remote prune origin
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment