Created
June 27, 2015 18:43
-
-
Save jatubio/d3a509e328ef3474f615 to your computer and use it in GitHub Desktop.
Special git cherry-pick with a git rebase interactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Juan Antonio Tubio <[email protected]> | |
# GitHub: https://github.com/jatubio | |
# Twitter: @jatubio | |
# | |
# | |
# Special git cherry-pick with a git rebase interactive | |
# To use on branch with the last-commit | |
# | |
# Parameters: hash of oldest commit to cherry-pick | |
# | |
# | |
if [ -z $1 ]; then | |
echo "Special git cherry-pick with a git rebase interactive."; | |
echo "Pass the hash of oldest commit to cherry-pick"; | |
echo ""; | |
echo "Usage: $0 hash"; | |
echo ""; | |
exit 1; | |
fi | |
# Abort any cherry-pick in progress | |
GIT_SEQUENCE_DIR=$(git rev-parse --git-dir)/sequencer | |
if [ -d $GIT_SEQUENCE_DIR ]; then | |
echo "Aborting any cherry-pick in progress" | |
echo "" | |
git cherry-pick --abort | |
fi | |
GIT_OLD_BRANCH=$(git current-branch) | |
GIT_LAST_COMMIT=$(git last-commit) | |
GIT_FIRST_COMMIT=$1 | |
GIT_TEMP_BRANCH=${GIT_OLD_BRANCH}_${GIT_LAST_COMMIT}_$RANDOM | |
GIT_BACKUP_BRANCH="backups/cpi/${GIT_OLD_BRANCH}" | |
# Make one backup | |
echo Making a Backup of $GIT_OLD_BRANCH onto ${GIT_BACKUP_BRANCH} | |
echo "" | |
if [ `git branch --list | grep ${GIT_BACKUP_BRANCH} -i` ]; then | |
git branch -D "${GIT_BACKUP_BRANCH}" > /dev/null | |
fi | |
git branch "${GIT_BACKUP_BRANCH}" $GIT_OLD_BRANCH | |
# Make a temporary branch from first commit to pick | |
echo Making a temporary branch from first commit to pick | |
echo "" | |
git branch $GIT_TEMP_BRANCH $GIT_FIRST_COMMIT | |
git checkout $GIT_TEMP_BRANCH | |
# Cherry pick all commits onto temporary branch | |
echo Cherry picking all commits onto temporary branch | |
echo "" | |
git cherry-pick $GIT_FIRST_COMMIT..$GIT_LAST_COMMIT | |
if [ $? -ne 0 ]; then | |
echo error on Cherry-picking | |
git branch -D "${GIT_TEMP_BRANCH}" | |
git checkout $GIT_OLD_BRANCH | |
exit 1 | |
fi | |
# Git rebase interactive all commits of temporary branch | |
echo Git rebase interactive all commits of temporary branch | |
echo "" | |
git rebase -i ${GIT_TEMP_BRANCH}~$(git first-rebase-commit-counts ${GIT_TEMP_BRANCH}) | |
if [ $? -ne 0 ]; then | |
echo error on Git rebase interactive | |
git branch -D "${GIT_TEMP_BRANCH}" | |
git checkout $GIT_OLD_BRANCH | |
exit 1 | |
fi | |
# Reset old branch head to temporary branch and remove temporary branch | |
echo Reset old branch head to temporary branch and remove temporary branch | |
echo "" | |
git checkout $GIT_OLD_BRANCH | |
git reset --hard $GIT_TEMP_BRANCH | |
git branch -D $GIT_TEMP_BRANCH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment