Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created June 13, 2016 09:27
Show Gist options
  • Save OliverJAsh/088ae37ce842e34971e6ecef8d6b7dc6 to your computer and use it in GitHub Desktop.
Save OliverJAsh/088ae37ce842e34971e6ecef8d6b7dc6 to your computer and use it in GitHub Desktop.
git apply-pr
#compdef git-apply-pr
_git-apply-pr() {
_git-branch
}
#!/bin/bash
# Applies the PR for a given branch via rebase, and delete branches (local and
# remote) afterwards.
# Defaults to current branch.
#
# Usage: git apply-pr [<branch-name>]
#
# Installation: ln -s git-apply-pr /usr/local/bin
set -exu pipefail
BRANCH=`git rev-parse --abbrev-ref HEAD`
HEAD=${1:-$BRANCH}
BASE=${2:-master}
TMP="tmp"
git log --oneline $BASE..$HEAD
echo
read -p "Rebase $HEAD into $BASE?"
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
git checkout $BASE \
&& git pull \
&& echo "Rebase" \
&& git checkout -b $TMP $HEAD \
&& git rebase $BASE \
&& git checkout $BASE \
&& git merge --ff-only $TMP \
&& echo "Delete branches" \
&& git branch -d $TMP \
&& git branch -D $HEAD \
&& git push origin :$HEAD \
&& git push
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment