Skip to content

Instantly share code, notes, and snippets.

@preaction
Last active August 29, 2015 13:58
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 preaction/9944919 to your computer and use it in GitHub Desktop.
Save preaction/9944919 to your computer and use it in GitHub Desktop.
cherry-pick a commit into one or more release branches
USAGE="
\t$(basename $0) <BRANCH>... -- <COMMIT>...\
"
usage() {
echo "Usage:"
echo -e $USAGE
}
if [[ "$#" -lt 3 ]]; then
echo "ERROR: At least 3 arguments are needed!"
usage
exit 1
fi
if [[ ! ( "$@" =~ '--' ) ]]; then
echo "ERROR: Could not find separator '--' in arguments!"
usage
exit 1
fi
# We must be in a clean repo!
error_clean() {
echo "ERROR: Working directory must be clean to cherry-pick!"
exit 1
}
set +e
git diff --exit-code >/dev/null || error_clean
git diff --cached --exit-code >/dev/null || error_clean
COMMITS=
VERSIONS=
GATHER_VERSIONS=1
for ARG in "$@"; do
if [[ "$ARG" == '--' ]]; then
GATHER_VERSIONS=0;
elif [[ "$GATHER_VERSIONS" == 1 ]]; then
VERSIONS="$VERSIONS $ARG"
else
COMMITS="$COMMITS $ARG"
fi
done
echo "VERSIONS: $VERSIONS"
echo "COMMITS: $COMMITS"
set -e
# Try `basename $(git symbolic-ref HEAD)` in the future?
CURRENT_BRANCH=`git branch | awk '/^\*/{print $2}'`
for VER in $VERSIONS; do
git checkout $VER
git cherry-pick $COMMITS
git push
done
git checkout $CURRENT_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment