Skip to content

Instantly share code, notes, and snippets.

@allex
Forked from nickboldt/gcpl.sh
Created August 14, 2020 04:34
Show Gist options
  • Save allex/bfd7d1c011fe0333f83077f70f42f31d to your computer and use it in GitHub Desktop.
Save allex/bfd7d1c011fe0333f83077f70f42f31d to your computer and use it in GitHub Desktop.
git cherry pick latest (or multiple latest) commits
#!/bin/bash
#
#script to make it easier to git cherry-pick the latest commit to another branch
numCommits=1 # but we can cherry-pick more than one if needs be
red="\033[1;31m"
norm="\033[0;39m"
grey="\033[0;37m"
green="\033[0;32m"
blue="\033[0;34m"
purple="\033[0;35m"
QUIET="-q"
if [[ "$#" -eq 0 ]]; then usage; fi
while [[ "$#" -gt 0 ]]; do
case $1 in
'-c') numCommits="$2"; shift 1;; # eg., 5 or 10 commits to show
'-s') sourceBranch="$2"; shift 1;; # eg., 6.19.x or master
'-t') targetBranch="$2"; shift 1;; # eg., master or 6.19.x
'-m') targetBranch="master";; # shortcut for push from current branch to master
'-v') QUIET="";; # more output, remove the -q flag
'--help') usage;;
esac
shift 1
done
function gitlg ()
{
numHistory=$1
echo
git --no-pager log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(blue)%aE%Creset %Cgreen(%cr)%Creset' --abbrev-commit --date=relative -${numHistory}
echo; echo
}
function usage ()
{
echo "
Usage: ./${0##*/}
--help | show help
-c numCommits | n commits to cherry-pick (default: 1)
-s sourceBranch | source branch to use for commits to cherry-pick (default: current branch = $(git rev-parse --abbrev-ref HEAD))
-t targetBranch | target branch to push cherry-picks)
-m | push to master branch
"
exit 0
}
if [[ ! $targetBranch ]]; then usage; fi
if [[ ! $sourceBranch ]]; then sourceBranch=$(git rev-parse --abbrev-ref HEAD); fi
gitlg ${numCommits}
# get the commits to cherry-pick
for ((i=0; i<${numCommits}; i++)); do
sha=$(git rev-parse --short=7 HEAD~${i})
shas="${sha} ${shas}"
# echo -e "Cherry-pick commit #${i}: ${red}${sha}${norm}"
done
git checkout ${QUIET} ${targetBranch}
git pull ${QUIET} origin ${targetBranch}
for sha in ${shas}; do
if [[ $QUIET != "-q" ]]; then
echo -e "${purple}-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-${norm}
"
fi
git cherry-pick ${sha}
done
if [[ $QUIET != "-q" ]]; then
echo -e "
${grey}-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-${norm}
If anything went wrong above, fix conflicts with
${red}git mergetool${norm}
Then:
${red}git cherry-pick --continue${norm}
When everything is clean, commit your changes.
${grey}-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-${norm}
Tips:
To undo last commit, use:
${blue}git reset --soft HEAD^${norm}
To undo everything, use:
${blue}git checkout -- .; git reset HEAD .; git clean -fdx${norm}
To switch back to your source branch:
${green}git checkout ${sourceBranch}${norm}
${grey}-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-${norm}"
fi
let numHistory=numCommits+3
gitlg ${numHistory}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment