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