Skip to content

Instantly share code, notes, and snippets.

@ChrisPenner
Last active February 12, 2021 15:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisPenner/d3a3110eeea71f2b3159a353b9345c44 to your computer and use it in GitHub Desktop.
Save ChrisPenner/d3a3110eeea71f2b3159a353b9345c44 to your computer and use it in GitHub Desktop.
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
Copy link

aspiers commented Apr 7, 2020

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