Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created November 22, 2012 18:11
Show Gist options
  • Save naholyr/4132423 to your computer and use it in GitHub Desktop.
Save naholyr/4132423 to your computer and use it in GitHub Desktop.
Using "hub" to automatically push & PR

git-pr

Part of my personal shortcuts for the usual pull-request-based workflow.

Setup

Given you have an "upstream" repository you need to work on:

  1. Fork it
  2. Clone your fork locally (repo "origin")
  3. Add remote "upstream" with git remote add upstream $URL (not required but it's easier for you to sync later)

OK, you're all set up. Now you need hub.

Usage

Then, on your daily work, you'll have to fix issues (new features, bugs, whatever it's generally an issue) open on upstream repository.

# synchronize masters, always better
git checkout master
git pull upstream master
git push origin master
# create your branch for the issue 42
git checkout -b fix-universe-42 # I like adding issue number in branch name, for memory

while not-fixed; do
  work && git add -p && git commit
done

# now git-pr in action: push, and replaces the issue with a pull-request on upstream repo :)
git-pr 42 upstreamGitHubUsername:branch

Permissions

You don't require admin permissions, but you may require commit permissions. I did not try this on a repos I did not have permissions to push in.

Setting upstream repository

The hub notation is quite unusual: it's "username:branch", or "username/repo:branch" whenever the repository has different name on your origin and your upstream. Working on many different repositories, I felt lazy about typing the target each time, so I used git config --local pr.upstream to store the target.

Let's say you forked universe/earth and are working on issue #42. You can either:

# append "universe:master" each time you'll use git-pr
git-pr 42 universe:master

or

# remember it once for all
git-pr-config universe:master
# Note: this simply adds those lines to your .git/config:
#[pr]
#        upstream = universe:master

# simplier for next calls
git-pr 42
# Usage: git-pr [issue] [target]
# Will push current local branch to "origin" repository if necessary
# Will then create a pull-request to "upstream:master" (or $target if provided) repository from this branch
# If $issue is provided, the pushed code will be attached to this issue instead of creating a new PR
function git-pr () {
local branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$(git log --oneline -- origin/$branch)" = "" -o $(git log --oneline -- origin/$branch..$branch 2> /dev/null | wc -l) -gt 0 ]; then
# Needs to push commits
git push origin $branch
fi
# Now we can PR
eval "hub pull-request ${1:+-i $1} -b ${2:-$(git config --local pr.upstream)}"
}
function git-pr-config () {
git config --local pr.upstream "$1"
}
@naholyr
Copy link
Author

naholyr commented Nov 26, 2012

Added a shortcut to store PR target with hub notation

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