Skip to content

Commit aee3477

Browse files
committed
updating docs with environent variable method
1 parent 8764233 commit aee3477

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

docs/pre-push-branch-check.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# 🔒 Prevent Accidental Pushes to `main`/`master` — With Emergency Override
22

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:
44

55
```
6-
--break_glass_to_push_main
6+
BREAK_GLASS_MAIN_PUSH=1
77
```
88

99
---
@@ -17,31 +17,29 @@ mkdir -p ~/.git-hooks
1717

1818
## 2. Create the Pre‑Push Hook
1919

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.
2222

2323
Open your existing hook:
2424
```bash
25-
vim ~/.git-hooks/pre-push
25+
my-favorite-text-editor ~/.git-hooks/pre-push
2626
```
2727

2828
Paste in:
2929
```bash
3030
#!/bin/sh
3131
branch="$(git symbolic-ref --short HEAD)"
32-
override_flag="--break_glass_to_push_main"
3332

3433
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"
42-
exit 1
43-
;;
44-
esac
34+
if [ "$BREAK_GLASS_MAIN_PUSH" = "1" ]; then
35+
echo "⚠️ Override used: pushing to '$branch'..."
36+
# allow push
37+
else
38+
echo "❌ Push to '$branch' is disabled locally."
39+
echo " If you REALLY need to, run:"
40+
echo " BREAK_GLASS_MAIN_PUSH=1 git push origin $branch"
41+
exit 1
42+
fi
4543
fi
4644
```
4745

@@ -68,14 +66,14 @@ git config --global core.hooksPath ~/.git-hooks
6866
```
6967
2. Try to push:
7068
```bash
71-
git push
69+
git push origin main
7270
```
73-
Should be **blocked**.
71+
➡ Should be **blocked**.
7472

7573
3. Use the override (emergency only):
7674
```bash
77-
git push --break_glass_to_push_main
75+
BREAK_GLASS_MAIN_PUSH=1 git push origin main
7876
```
79-
Allowed with a warning.
77+
➡ Allowed with a warning.
8078

8179
---

0 commit comments

Comments
 (0)