You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pre-push-branch-check.md
+18-20Lines changed: 18 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# 🔒 Prevent Accidental Pushes to `main`/`master` — With Emergency Override
2
2
3
-
This guide sets up a **global Git pre‑push hook** that blocks pushes to protected branches (`main` and `master`) unless you explicitly use the override flag:
3
+
This guide sets up a **global Git pre‑push hook** that blocks pushes to protected branches (`main` and `master`) unless you explicitly set an environment variable override:
4
4
5
5
```
6
-
--break_glass_to_push_main
6
+
BREAK_GLASS_MAIN_PUSH=1
7
7
```
8
8
9
9
---
@@ -17,31 +17,29 @@ mkdir -p ~/.git-hooks
17
17
18
18
## 2. Create the Pre‑Push Hook
19
19
20
-
Before adding this new hook, check if ~/.git-hooks/pre-push already exists.
21
-
If it does, do not overwrite it — instead, add the branch‑protection logic to your current hook so both sets of checks run.
20
+
Before adding this new hook, check if `~/.git-hooks/pre-push` already exists.
21
+
If it does, **do not overwrite it** — instead, merge the branch‑protection logic into your current hook so both sets of checks run.
22
22
23
23
Open your existing hook:
24
24
```bash
25
-
vim~/.git-hooks/pre-push
25
+
my-favorite-text-editor~/.git-hooks/pre-push
26
26
```
27
27
28
28
Paste in:
29
29
```bash
30
30
#!/bin/sh
31
31
branch="$(git symbolic-ref --short HEAD)"
32
-
override_flag="--break_glass_to_push_main"
33
32
34
33
if [ "$branch"="main" ] || [ "$branch"="master" ];then
35
-
case"$*"in
36
-
*"$override_flag"* )
37
-
echo"⚠️ Override used: pushing to '$branch'..."
38
-
;;
39
-
*)
40
-
echo"❌ Push to '$branch' is disabled locally."
41
-
echo" If you **really** need to, use: git push $override_flag"
0 commit comments