Last active
February 12, 2021 15:26
-
-
Save ChrisPenner/d3a3110eeea71f2b3159a353b9345c44 to your computer and use it in GitHub Desktop.
Transplant commits from one source to another
This file contains 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 | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://blog.adamspiers.org/2018/06/14/git-auto-magic/