Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emil-alexandrescu/a2a856ad8fdc4c95b4ce608eeced5bcf to your computer and use it in GitHub Desktop.
Save emil-alexandrescu/a2a856ad8fdc4c95b4ce608eeced5bcf to your computer and use it in GitHub Desktop.
Git Mass Cherry Pick
# Add the function to the .zshrc and update your console environment
function git-mass-cherry-pick {
# The exact commit from which the cherry-picking will start.
readonly start=${1:?"Starting commit SHA must be specified."}
local branch_name=$(git rev-parse --abbrev-ref HEAD);
local bak_name="bak/${branch_name}"
# Remember commits to be cherry-picked
local commits_to_cherry_pick=()
for commit in $(git log $start^1.. --no-merges --reverse --pretty=%H --abbrev-commit);
do
commits_to_cherry_pick+=$commit
done
# Check if bak_name branch already exists
if [[ $(git branch --list $bak_name) ]]
then
echo "bak/ prefixed branch already exists. Deleting it"
git branch -D $bak_name
fi
# Backup current branch
git checkout -b $bak_name
# git checkout $branch_name
# Update develop branch
git checkout develop
git fetch && git pull --rebase origin develop
# Delete original current branch
git branch -D $branch_name
# Clone develop to apply cherry-picking on top of it
git checkout -b $branch_name
# Apply cherry-pick one by one
for commit in $commits_to_cherry_pick;
do
git cherry-pick $commit
###########################################################
# IMPORTANT: if want to auto resolve cherry-pick conflicts
###########################################################
# git cherry-pick --strategy=recursive -X theirs
done
}
# USAGE: Run in command line
$ git-mass-cherry-pick c47ee8bb0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment