Skip to content

Instantly share code, notes, and snippets.

@takkaria
Created April 13, 2021 16:20
Show Gist options
  • Save takkaria/dfd0f1c1e11745f1c2f9f6f986e3a908 to your computer and use it in GitHub Desktop.
Save takkaria/dfd0f1c1e11745f1c2f9f6f986e3a908 to your computer and use it in GitHub Desktop.
cherry-branch
cherry-branch () {
if (test $# -lt 3); then
echo "Usage: cherry-branch [-p] <branch> <origin> <refs...>";
echo " -p push to upstream with same name"
echo " <branch> new branch name"
echo " <origin> origin ref, e.g. origin/master"
echo " <refs> commit IDs to cherry-pick"
return;
fi
if (test "$1" == "-p"); then
PUSH=1
shift
else
PUSH=0
fi
local BRANCH_NAME=$1;
local ORIGIN_REF=$2;
shift 2;
git checkout -b $BRANCH_NAME || (echo "*** Checkout failed" && return)
git reset --hard $ORIGIN_REF || (echo "*** Reset failed" && return)
while (( "$#" )); do
git cherry-pick $1 || (echo "*** Cherry-picking failed" && return)
shift
done
if (test $PUSH -eq 1); then
git push --set-upstream origin $BRANCH_NAME
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment