Skip to content

Instantly share code, notes, and snippets.

@tw3
Last active January 27, 2022 17:18
Show Gist options
  • Save tw3/bc71221aed89068fd371a2bf2ccf6062 to your computer and use it in GitHub Desktop.
Save tw3/bc71221aed89068fd371a2bf2ccf6062 to your computer and use it in GitHub Desktop.
Bash script to cherry pick any new commits in a reference branch to a source branch. Use at your own risk!
#!/usr/bin/env bash
TARGET_BRANCH=$(git branch --show-current)
if [ -z "${TARGET_BRANCH}" ]; then
echo "Please cd into to the target branch"
exit;
fi
echo "Cherry-picking to branch: ${TARGET_BRANCH}"
read -p "Cherry-picking from branch: " SOURCE_BRANCH
if [ -z "${SOURCE_BRANCH}" ]; then
echo
echo "Please specify a source branch, exiting"
exit;
fi
read -p "Reference branch [pilot]: " REF_BRANCH
REF_BRANCH=${REF_BRANCH:-pilot}
if [ -z "${REF_BRANCH}" ]; then
echo
echo "Please specify a reference branch, exiting"
exit;
fi
HASHES_RESULTS=$(git log --no-merges --oneline --format=format:"%H" ${SOURCE_BRANCH} ^${REF_BRANCH} | tr '\n' ' ')
if [ -z "${HASHES_RESULTS}" ]; then
echo
echo "There are no differences between ${SOURCE_BRANCH} and ${REF_BRANCH}"
echo
echo "Exiting"
exit;
fi
PREVIEW_CMD="git log --no-merges --oneline ${SOURCE_BRANCH} ^${REF_BRANCH}"
echo
echo "Commmits (${PREVIEW_CMD}):"
eval ${PREVIEW_CMD}
CHERRY_PICK_CMD="git cherry-pick ${HASHES_RESULTS}"
echo
echo "Going to run command:"
echo
echo "${CHERRY_PICK_CMD}"
echo
read -p "Proceed? (y/n): " PROCEED
if [ "${PROCEED}" != "y" ]; then
echo
echo "Aborting"
exit;
fi
exec ${CHERRY_PICK_CMD}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment