Skip to content

Instantly share code, notes, and snippets.

@brennemankyle
Last active May 10, 2024 14:47
Show Gist options
  • Save brennemankyle/6770781bc43c8d2b03988c4c89867871 to your computer and use it in GitHub Desktop.
Save brennemankyle/6770781bc43c8d2b03988c4c89867871 to your computer and use it in GitHub Desktop.
# Have you ever had multiple branches stacked on top of each other waiting for code review
# and need to merge origin/master (or some other change) up the chain through each of them.
# This script will do that!
# Example: merge_through_each master branch_parent branch_child branch_grand_child
run_command () {
echo ""
echo "running: $@"
if ! $@
then
echo "^ command failed: $@"
exit 1
fi
}
# Get all params
branches=()
while [ "$1" != "" ]; do
branches+=($1)
last=$1
shift # Shift all the parameters down by one
done
# Explain what will happen
echo ""
echo "run for each branch from left to right: "
echo " git checkout second_branch"
echo " git pull"
echo " git merge origin/first_branch --no-edit"
echo " git push"
echo -n "continue in order: "
for branch in ${branches[@]}; do
if [ $branch == $last ]; then
echo "$branch"
else
echo -n "$branch > "
fi
done
echo ""
echo -n "respond Y to confirm: "
# Ask to continue
read response
if [ "$response" != "y" ] && [ "$response" != "Y" ]; then
exit 1
fi
echo ""
echo ""
# Merge Each
prev=""
for branch in ${branches[@]}; do
if [ "$prev" == "" ]; then
prev=$branch
continue
fi
run_command git checkout $branch
run_command git pull
run_command git merge origin/$prev --no-edit
run_command git push
prev=$branch
done
echo ""
echo "Success! Everything merged"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment