Skip to content

Instantly share code, notes, and snippets.

@ChrisPenner

ChrisPenner/git-transplant

Last active Feb 12, 2021
Embed
What would you like to do?
Transplant commits from one source to another
#!/bin/bash
if [[ $# -ne 2 ]]; then
cat >&2 <<EOF
Transplant a branch from one root to another.
Handy if you've done a squash-merge and need to rebase <from> the old branch <onto> the new master.
Usage:
git transplant <from> <to>
where:
from: The commit or branch where your 'keep' commits will start from. The <from> commit itself is NOT kept, only its children
to: The branch onto which the commits between (<from>..HEAD] will be rebased.
EOF
exit 1
fi
from="$1"
to="$2"
echo "Transplanting these commits:"
git log --pretty=format:'%Cblue%h%Creset %Cgreen%<(15)%ad%Creset | %s%d [%C(yellow)%an%Creset] %C(cyan)%d%Creset' --date=relative "$from"..HEAD
echo
echo '================================================================================'
echo
echo "Which were previously based on:"
git log -1 --pretty=format:'%Cblue%h%Creset %Cgreen%<(15)%ad%Creset | %s%d [%C(yellow)%an%Creset] %C(cyan)%d%Creset' --date=relative -1 "$from"
echo
echo '================================================================================'
echo
echo "Onto:"
echo
git log -1 --pretty=format:'%Cblue%h%Creset %Cgreen%<(15)%ad%Creset | %s%d [%C(yellow)%an%Creset] %C(cyan)%d%Creset' --date=relative -1 "$to"
echo
echo '================================================================================'
echo
echo "Which will remove the following commits:"
echo
git log --pretty=format:'%Cblue%h%Creset %Cgreen%<(15)%ad%Creset | %s%d [%C(yellow)%an%Creset] %C(cyan)%d%Creset' --date=relative "$to".."$from"
echo
echo '================================================================================'
echo
read -p "Continue? (y/n)
" -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1; fi
git rebase --onto "$to" "$from"
@aspiers

This comment has been minimized.

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