Skip to content

Instantly share code, notes, and snippets.

@berndverst
Created October 6, 2022 20:29
Show Gist options
  • Save berndverst/5a12d202d8ab35ee48e0e11ddd4a833a to your computer and use it in GitHub Desktop.
Save berndverst/5a12d202d8ab35ee48e0e11ddd4a833a to your computer and use it in GitHub Desktop.
Update PR remote branch checked out via Github CLI (gh pr checkout)

Compendium function for updating PRs checked out via GitHub CLI

This script is called updatePR.

Usage:

gh pr checkout 1234
# make some changes
updatePR 1234

You will be prompted to confirm whether you want to push commits to the remote branch from which the PR originated. This requires that you have write access to that destination.

How to add this function:

Add the following to your .zshrc or .bashrc.

updatePR () {
destination=$(gh pr view $1 --json headRepositoryOwner,headRepository,headRefName | jq -r '"git@github.com:" + .headRepositoryOwner.login + "/" + .headRepository.name + ".git HEAD:" + .headRefName')
echo "Pushing to ${destination}"
read confirm"?Are you sure? Type [y] to continue: "
echo
if [[ $confirm =~ ^[Yy]$ ]]
then
 $(echo "git push ${destination}")
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment