-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathgit-main-branch
More file actions
executable file
·33 lines (27 loc) · 858 Bytes
/
git-main-branch
File metadata and controls
executable file
·33 lines (27 loc) · 858 Bytes
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
#!/bin/sh
set -eu
# Bail early with git's own error message if not in a repo
git is-repo
probe_branch_name() {
local full_ref="$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null)"
local branch_name="${full_ref#refs/remotes/origin/}"
if [ -n "$branch_name" ]; then
echo "$branch_name"
exit 0
fi
}
# Step 1: Check what the origin's HEAD points to (fast, local-only)
probe_branch_name
# Step 2: If no origin exists (e.g. newly initialized repo), try common branch names
for probe in main mainline master production; do
if git local-branch-exists "$probe"; then
echo "$probe"
exit 0
fi
done
# Step 3: Ask the remote for its HEAD (expensive network call, but only needed once)
if git remote set-head origin --auto 2>/dev/null; then
probe_branch_name
fi
echo "No main branch found" >&2
exit 2