Skip to content

Commit bbd8b0e

Browse files
committed
Adding per-repo instructions to the doc
1 parent aee3477 commit bbd8b0e

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

docs/pre-push-branch-check.md

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
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 set an environment variable override:
3+
This guide shows you how to install a **Git pre‑push hook** that blocks pushes to branches (`main` or `master`) unless you explicitly set a noisy environment variable:
44

55
```
66
BREAK_GLASS_MAIN_PUSH=1
77
```
88

9-
---
10-
11-
## 1. Create a Global Git Hooks Directory
12-
```bash
13-
mkdir -p ~/.git-hooks
14-
```
9+
You can install this hook **globally** (affecting all your repos) or **per repo** (only in the specific repo you choose).
10+
Pick the option that best fits your workflow.
1511

1612
---
1713

18-
## 2. Create the Pre‑Push Hook
14+
## 🛠 The Hook Script
1915

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.
16+
Both installation methods use the same script:
2217

23-
Open your existing hook:
24-
```bash
25-
my-favorite-text-editor ~/.git-hooks/pre-push
26-
```
27-
28-
Paste in:
2918
```bash
3019
#!/bin/sh
3120
branch="$(git symbolic-ref --short HEAD)"
3221

3322
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
3423
if [ "$BREAK_GLASS_MAIN_PUSH" = "1" ]; then
3524
echo "⚠️ Override used: pushing to '$branch'..."
36-
# allow push
3725
else
3826
echo "❌ Push to '$branch' is disabled locally."
3927
echo " If you REALLY need to, run:"
@@ -45,35 +33,67 @@ fi
4533

4634
---
4735

48-
## 3. Make the Hook Executable
49-
```bash
50-
chmod +x ~/.git-hooks/pre-push
51-
```
36+
## Option 1 — Install Globally (All Repos)
5237

53-
---
38+
This will protect every repo on your machine by default.
5439

55-
## 4. Tell Git to Use It Globally
56-
```bash
57-
git config --global core.hooksPath ~/.git-hooks
58-
```
40+
1. Create a global hooks directory:
41+
```bash
42+
mkdir -p ~/.git-hooks
43+
```
44+
45+
2. Create the pre‑push hook:
46+
```bash
47+
vim ~/.git-hooks/pre-push
48+
```
49+
Paste the script above.
50+
51+
3. Make it executable:
52+
```bash
53+
chmod +x ~/.git-hooks/pre-push
54+
```
55+
56+
4. Tell Git to use it globally:
57+
```bash
58+
git config --global core.hooksPath ~/.git-hooks
59+
```
5960

6061
---
6162

62-
## 5. Test It
63-
1. Switch to `main`:
63+
## Option 2 — Install Per Repo (Only One Project)
64+
65+
This will protect only the repo you set it up in.
66+
67+
1. Go to your repo:
6468
```bash
65-
git checkout main
69+
cd /path/to/your-repo
6670
```
67-
2. Try to push:
71+
72+
2. Create the pre‑push hook:
73+
```bash
74+
vim .git/hooks/pre-push
75+
```
76+
Paste the script above.
77+
78+
3. Make it executable:
79+
```bash
80+
chmod +x .git/hooks/pre-push
81+
```
82+
83+
---
84+
85+
## ✅ Testing
86+
87+
1. Try pushing to `main` without override:
6888
```bash
6989
git push origin main
7090
```
71-
➡ Should be **blocked**.
91+
➡ Should be **blocked**.
7292

73-
3. Use the override (emergency only):
93+
2. Try with override:
7494
```bash
7595
BREAK_GLASS_MAIN_PUSH=1 git push origin main
7696
```
77-
➡ Allowed with a warning.
97+
➡ Allowed with warning.
7898

7999
---

0 commit comments

Comments
 (0)