Skip to content

Instantly share code, notes, and snippets.

@gaqzi
Created March 5, 2021 03:24
Show Gist options
  • Save gaqzi/d362a4e3acddd3276a81d28e094cf0e3 to your computer and use it in GitHub Desktop.
Save gaqzi/d362a4e3acddd3276a81d28e094cf0e3 to your computer and use it in GitHub Desktop.
A script to help merge multiple branches based off each other. For dealing with slow reviews but still trying to make small behavior or structural change PRs.
#!/bin/bash
: ${DRY_RUN:=true}
if [ $# -lt 2 ]; then
echo "Usage: git merge-train <first branch> <branch based on first branch> [... branches based on previous]"
exit 0
fi
if [ "${DRY_RUN}" != 'false' ]; then
echo "In dry run mode. To rebase the entire merge train run:"
echo " DRY_RUN=false git merge-train $@"
fi
branch_before=$(git branch --show-current)
parent_branch=master
for branch in ${@}; do
echo git merge-base --fork-point $parent_branch $branch
fork_point=$(git merge-base --fork-point $parent_branch $branch)
if [ $? -ne 0 ]; then
echo "getting merge base failed, aborting: ${fork_point}" >&2
exit 1
fi
git checkout ${branch}
if [ "${DRY_RUN}" != 'false' ]; then
echo "git rebase --onto ${parent_branch} ${fork_point}"
else
git rebase --onto ${parent_branch} ${fork_point}
fi
parent_branch=$branch
done
git checkout ${branch_before}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment