Git: Keep two branches in sync
I have a Git repository with a primary branch that I maintain actively. I also have a separate feature branch that I'd like to keep in sync with the main branch, and alert if there are any conflicts immediately. Using Git hooks you can script automatically pulling each new commit from the primary branch to the feature branch.
Create a .git/hooks/post-commit file and put these contents in it:
#!/bin/sh
SRC="main"
DST="feature"
branch=$(git branch --show-current)
if [ "$branch" = "$SRC" ]; then
commit=$(git rev-parse HEAD)
git checkout $DST &&
git cherry-pick "$commit" &&
git checkout main
fi
If there are conflicts, Git errors out immediately and leaves you on the feature branch to manually resolve the conflict.
Tags:


