Skip to content

Instantly share code, notes, and snippets.

@d3lm
Last active January 26, 2018 10:59
Show Gist options
  • Save d3lm/3604d45ab3d154864adfec53b1019bde to your computer and use it in GitHub Desktop.
Save d3lm/3604d45ab3d154864adfec53b1019bde to your computer and use it in GitHub Desktop.
Git workflow to checkout or update pull requests
show_branch() {
echo $(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/')
}
update_pr() {
CURRENT_BRANCH=$(show_branch)
IFS='/' read -ra array <<< "$CURRENT_BRANCH"
PR_ID=${array[1]}
if [ "${array[0]}" != "pr" ]
then
echo "You are currently not on PR. Make sure to check out a PR first."
return 1
fi
git pr $PR_ID $1
}
...
[alias]
...
pr = "!f() { git fetch -fu ${2:-origin} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
pru = !bash -c 'source $HOME/.git-functions.sh && update_pr $1'
prc = "!git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done"

Git Pull Request Management

The following git aliases help you to easily checkout and update pull requests.

The main commands git pr [id] [remote?] and git prc were taken from from this gist, so all credits for the original git aliases go to the author.

Note, the question mark indicates optional parameters.

However, I have added a new command git pru [remote?] which lets you update a PR. You can also do this by calling git pr [id] again but to make this more convenient I have implemented an alias that uses the original command and parses the current branch name to automatically pass along the PR id. If you are not currently on a branch that starts with pr/ you'll get an error.

Usage

// Checkout PR with id 123, assuming the remote is origin
git pr 123

// Author of the PR has pushed some updates so we want to update our PR branch accordingly
git pru
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment