Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Last active August 25, 2017 11:25
Show Gist options
  • Save WimObiwan/c5f42c91a6735e9559a482f8cd3466f1 to your computer and use it in GitHub Desktop.
Save WimObiwan/c5f42c91a6735e9559a482f8cd3466f1 to your computer and use it in GitHub Desktop.
Hooks to automerge (fix) branches
#!/bin/sh
# Put in directory .git/hooks
branch=$(git rev-parse --abbrev-ref --quiet HEAD)
echo "Running automerge for branch $branch"
config_automergeto="branch.$branch.automergeto"
automergeto=$(git config $config_automergeto)
if [ -n "$automergeto" ]; then
echo "Auto merge to these branches: $automergeto"
for automergeto_branch in $automergeto
do
echo '*******************************************************************************'
echo "** Auto merging from $branch to $automergeto_branch"
git checkout $automergeto_branch && git pull --ff-only && git merge $branch
if [[ $? != 0 ]]; then
echo "** AUTOMERGE from $branch to $automergeto_branch FAILED !!!"
git merge --abort
fi
echo '*******************************************************************************'
done
git checkout $branch
else
echo "Automerge disabled (no config value for $config_automergeto)"
fi
#!/bin/sh
# Explanation
#
# 1/ Put the 3 scripts (2 hooks) in .git/hooks of your repository
# 2/ Adjust .git/config to set merge targets:
# e.g.
#
# [branch "fix-MyFixBranch"]
# remote = origin
# merge = refs/heads/fix-MyFixBranch
# automergeto = MyReleaseBranch master
#
# (the last line is needs to be added)
#
# This also works recusively (when merge target is again configured to merge through to other branches).
# Tested with command line & GIT Extensions.
#!/bin/sh
# Put in directory .git/hooks
echo 'Begin executing post-commit hook'
./.git/hooks/automerge
echo 'Done executing post-commit hook'
#!/bin/sh
# Put in directory .git/hooks
echo 'Begin executing post-merge hook'
./.git/hooks/automerge
echo 'Done executing post-merge hook'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment