Skip to content

Instantly share code, notes, and snippets.

@emlys
Created July 16, 2021 19:29
Show Gist options
  • Save emlys/6f002aa71a76ca1c4bea4f262b390eba to your computer and use it in GitHub Desktop.
Save emlys/6f002aa71a76ca1c4bea4f262b390eba to your computer and use it in GitHub Desktop.
git post-commit hook to keep one branch updated with changes from another
#!/bin/sh
# post-commit hook to keep one branch updated with all the changes from another
# so that the target branch always has a superset of the changes in the source branch
# do this by rebasing target off of source after each commit to source
SOURCE_BRANCH=example/generate-docs
BRANCH_TO_REBASE=task/31/models-A-D
# get the current branch in the format "* branch_name"
CURRENT_BRANCH_LINE=$(git branch | grep "*")
# get everything except the "* "
CURRENT_BRANCH_NAME=${CURRENT_BRANCH_LINE:2}
# if we're on the source branch, update the target branch with this new commit
if [ "$CURRENT_BRANCH_NAME" = "$SOURCE_BRANCH" ]; then
echo "Updating $BRANCH_TO_REBASE with this new commit"
echo ""
# make sure we don't miss any commits that are only on remote
echo "Checking for updates on remote..."
git pull
echo ""
# check out the target and rebase onto the source branch
echo "Rebasing $BRANCH_TO_REBASE off of $SOURCE_BRANCH..."
git checkout $BRANCH_TO_REBASE
git rebase $SOURCE_BRANCH
echo ""
# return to the source branch we started on
echo "Returning to $SOURCE_BRANCH..."
git checkout $SOURCE_BRANCH
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment