|
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 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: |
4 | 4 |
|
5 | 5 | ``` |
6 | 6 | BREAK_GLASS_MAIN_PUSH=1 |
7 | 7 | ``` |
8 | 8 |
|
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. |
15 | 11 |
|
16 | 12 | --- |
17 | 13 |
|
18 | | -## 2. Create the Pre‑Push Hook |
| 14 | +## 🛠 The Hook Script |
19 | 15 |
|
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: |
22 | 17 |
|
23 | | -Open your existing hook: |
24 | | -```bash |
25 | | -my-favorite-text-editor ~/.git-hooks/pre-push |
26 | | -``` |
27 | | - |
28 | | -Paste in: |
29 | 18 | ```bash |
30 | 19 | #!/bin/sh |
31 | 20 | branch="$(git symbolic-ref --short HEAD)" |
32 | 21 |
|
33 | 22 | if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then |
34 | 23 | if [ "$BREAK_GLASS_MAIN_PUSH" = "1" ]; then |
35 | 24 | echo "⚠️ Override used: pushing to '$branch'..." |
36 | | - # allow push |
37 | 25 | else |
38 | 26 | echo "❌ Push to '$branch' is disabled locally." |
39 | 27 | echo " If you REALLY need to, run:" |
|
45 | 33 |
|
46 | 34 | --- |
47 | 35 |
|
48 | | -## 3. Make the Hook Executable |
49 | | -```bash |
50 | | -chmod +x ~/.git-hooks/pre-push |
51 | | -``` |
| 36 | +## Option 1 — Install Globally (All Repos) |
52 | 37 |
|
53 | | ---- |
| 38 | +This will protect every repo on your machine by default. |
54 | 39 |
|
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 | + ``` |
59 | 60 |
|
60 | 61 | --- |
61 | 62 |
|
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: |
64 | 68 | ```bash |
65 | | - git checkout main |
| 69 | + cd /path/to/your-repo |
66 | 70 | ``` |
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: |
68 | 88 | ```bash |
69 | 89 | git push origin main |
70 | 90 | ``` |
71 | | - ➡ Should be **blocked**. |
| 91 | + ➡ Should be **blocked**. |
72 | 92 |
|
73 | | -3. Use the override (emergency only): |
| 93 | +2. Try with override: |
74 | 94 | ```bash |
75 | 95 | BREAK_GLASS_MAIN_PUSH=1 git push origin main |
76 | 96 | ``` |
77 | | - ➡ Allowed with a warning. |
| 97 | + ➡ Allowed with warning. |
78 | 98 |
|
79 | 99 | --- |
0 commit comments