Skip to content

Instantly share code, notes, and snippets.

@kekssw
Last active August 29, 2015 14:10
Show Gist options
  • Save kekssw/2fb6cdc24c538ac33d1b to your computer and use it in GitHub Desktop.
Save kekssw/2fb6cdc24c538ac33d1b to your computer and use it in GitHub Desktop.
git-transplant
#!/bin/bash
if [ "$#" -lt 2 ]
then
cat << EOM
Usage: git-transplant <FROM> <TO> <UPSTREAM>
Transplants <FROM> .. <TO> commit range (inclusive) on top of <UPSTREAM>.
If <FROM> is a number, transplant that number of commits ending at <TO> instead
(i.e. <TO>~<FROM> .. <TO> range).
<UPSTREAM> defaults to current branch (HEAD).
EOM
exit 1
fi
DEPTH=$1
TO=$2
if [ "$DEPTH" -eq "$DEPTH" ] 2>/dev/null
then
FROM=$TO~$DEPTH;
else
FROM=$DEPTH~1
fi
UPSTREAM=${3-HEAD}
BRANCH=$(git rev-parse --abbrev-ref $UPSTREAM)
ONTO=$(git rev-list $UPSTREAM --abbrev-commit -1)
echo -e "\nTransplanting $FROM..$TO onto $BRANCH (was at $ONTO)\n"
set -x
git checkout $TO
git branch -f $BRANCH
git rebase -i --onto $ONTO $FROM $BRANCH;
set +x
@sedovmik
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment