Skip to content

Instantly share code, notes, and snippets.

@pmilovanov
Created July 7, 2022 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmilovanov/697e9a5dca8ce20d09e4d061d7fa9c1b to your computer and use it in GitHub Desktop.
Save pmilovanov/697e9a5dca8ce20d09e4d061d7fa9c1b to your computer and use it in GitHub Desktop.
Never again look up a PR's branch

TL;DR

Tired of looking up what a PR branch is to merge, rebase, or checkout? With these few utils, you can

  • checkout PR 135: checkout-pr 135

  • merge PR 135 into current branch: merge-pr 135

  • rebase current branch on branch of PR 135: rebase-on-pr 135

Steps

  1. Install and setup github cli (https://github.com/cli/cli):

     brew install gh
     gh auth
    
  2. Paste the functions below to your ~/.zshrc or ~/.bashrc:

    function prbranch {
        gh pr view $1 --json headRefName | jq -r .headRefName
    }
    function merge-pr {
        branch="$(prbranch $1)"
        git fetch origin "$branch:$branch" &&
        git merge "$branch"
    }
    function rebase-on-pr {
        branch="$(prbranch $1)"
        git fetch origin "$branch:$branch" &&
        git rebase "$branch"
    }
    function checkout-pr {
      branch="$(prbranch $1)"
        git fetch origin "$branch:$branch" &&
        git checkout "$branch"
    }
    
  3. Navigate to the repo dir and now you can use the commands at the top of this readme.

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