Skip to content

Instantly share code, notes, and snippets.

@PaulRBerg
Created May 21, 2023 15:10
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 PaulRBerg/cc9230fb993ea5f27aa328861f38e3c5 to your computer and use it in GitHub Desktop.
Save PaulRBerg/cc9230fb993ea5f27aa328861f38e3c5 to your computer and use it in GitHub Desktop.
Checkout a temporary branch, cherry pick the provided commit(s), and finally switch to the provided branch name
# Checkout a temporaru branch `tmp`, cherry pick the provided commit(s), and
# finally switch to the provided branch name
# $1 = branch name
# $2 = first commit
# $3 = second commit for forming a range (optional)
function goCherryPick() {
if [ -z "$1" ]; then
echo "Branch name not provided, aborting"
return;
fi
if [ "$1" = "tmp" ]; then
echo "The branch name cannot be tmp"
fi
if [ -z "$2" ]; then
echo "Commit not provided, aborting"
return;
fi
git checkout -b tmp
if [ -z "$3" ]; then
git cherry-pick "$2"
else
git cherry-pick "$2"^.."$3"
fi
git branch -D "$1"
git branch -m "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment