Git Pre Push Hook - Real World Use Cases and Examples

🔍 pre-push Hook — What Does It Do?

The pre-push Git hook runs just before code is pushed to a remote repository. It gives you a final chance to validate or block the push if certain conditions are not met.

Location: .git/hooks/pre-push
✅ Can prevent the push if any check fails
🧠 Great for teams and CI-safe workflows


🧠 When Is It Useful?

Whenever you want to stop bad code or files from reaching the remote repository:

  • Broken or failing tests
  • Invalid commit messages
  • Pushing to the wrong branch (like main)
  • Secrets or credentials being leaked
  • Accidentally pushing large files

🎯 Real-World Use Cases for pre-push

1. 🧪 Run Tests Before Push

Block the push if tests fail:

#!/bin/bash
echo "🚀 Running tests before push..."
npm test

if [ $? -ne 0 ]; then
  echo "❌ Tests failed. Push aborted!"
  exit 1
fi

2. ⛔ Prevent Push to Protected Branch (e.g., main)

Avoid accidental pushes to production branches:

#!/bin/bash
branch=$(git rev-parse --abbrev-ref HEAD)

if [[ "$branch" == "main" || "$branch" == "master" ]]; then
  echo "❌ Direct push to '$branch' is blocked!"
  exit 1
fi

3. 🧼 Run Linter Before Push

Maintain consistent code style by running linters:

#!/bin/bash
echo "📏 Running ESLint..."
npx eslint .

if [ $? -ne 0 ]; then
  echo "❌ Lint errors found. Push blocked."
  exit 1
fi

4. 🧾 Validate Commit Messages

Check the format of all commits being pushed:

#!/bin/bash
commits=$(git log origin/main..HEAD --pretty=format:"%s")

for msg in $commits; do
  if [[ "$msg" != feat:* && "$msg" != fix:* ]]; then
    echo "❌ Commit '$msg' is not in valid format."
    exit 1
  fi
done

5. 🔐 Scan for Secrets Before Push

Prevent pushing passwords or keys using detect-secrets:

#!/bin/bash
echo "🔐 Checking for secrets..."
detect-secrets-hook --baseline .secrets.baseline

✅ Requires setting up detect-secrets with a baseline file


6. 🧱 Block Large Files

Prevent files larger than 5MB from being pushed:

#!/bin/bash
echo "📦 Checking file sizes..."

big_files=$(git diff --cached --name-only | xargs -I{} find {} -size +5M 2>/dev/null)

if [ -n "$big_files" ]; then
  echo "❌ Large files detected:"
  echo "$big_files"
  exit 1
fi

📦 Summary Table

Use CaseBenefit
Run testsAvoid pushing broken code
Block push to protected branchPrevent accidents on main/prod
Lint checkMaintain clean, consistent code
Commit message validationEnforce team commit message guidelines
Secret scannerPrevent accidental token/password leak
File size checkAvoid uploading large or binary files

🔚 Final Note

The pre-push hook is one of the most powerful safety nets in Git. It ensures:

  • ✅ Only clean, tested, and secure code reaches the remote
  • 🛡️ Team rules are enforced before the code leaves your local machine

👨‍🏫 Want a Complete Demo Project?

If you'd like:

  • A Git project with working examples of pre-commit, commit-msg, and pre-push hooks
  • Student-friendly scripts with README docs
  • Published GitHub repo to clone or fork

Just message me: "demo bana bhai" 😄 I'll share the complete project link with full setup and explanation!



Let me know if you want this merged into a **Git Hooks Master Series** or need it visualized with Mermaid diagrams and Git workflows!