Skip to content

Commit 8966b14

Browse files
feat: add post-checkout hook to merge latest main/master into new local branches
1 parent 9c2a813 commit 8966b14

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

.husky/post-checkout

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env sh
2+
# When switching to a branch that doesn't exist on remote (e.g. newly created),
3+
# pull and merge origin/main or origin/master into current branch. Does not push.
4+
5+
# Only run on branch checkout (not file checkout)
6+
if [ "$3" != "1" ]; then
7+
exit 0
8+
fi
9+
10+
# Skip if we don't have a remote
11+
if ! git rev-parse --verify origin 2>/dev/null; then
12+
exit 0
13+
fi
14+
15+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
16+
17+
# Skip main/master/development - no need to merge base into these
18+
case "$CURRENT_BRANCH" in
19+
main|master|development) exit 0 ;;
20+
esac
21+
22+
# Only run when current branch does not exist on origin (treat as new local branch)
23+
if git ls-remote --heads origin "$CURRENT_BRANCH" 2>/dev/null | grep -q .; then
24+
exit 0
25+
fi
26+
27+
# Prefer main, fallback to master
28+
if git rev-parse --verify origin/main 2>/dev/null; then
29+
BASE=origin/main
30+
elif git rev-parse --verify origin/master 2>/dev/null; then
31+
BASE=origin/master
32+
else
33+
exit 0
34+
fi
35+
36+
echo "New branch detected: merging latest $BASE into $CURRENT_BRANCH (local only, not pushing)..."
37+
git fetch origin
38+
git merge "$BASE" --no-edit --no-ff
39+
echo "Done. Merge is local only; push when ready."

0 commit comments

Comments
 (0)