Skip to content

Latest commit

 

History

History
144 lines (122 loc) · 10.7 KB

File metadata and controls

144 lines (122 loc) · 10.7 KB

Complete guide to GitHooks - Creating your own pre-commit

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to Git Hooks

Git hooks let you automate scripts at specific points in your Git workflow, like before commits or pushes. They run locally on your machine or on the server side, and can block actions if conditions fail. Common ones include pre-commit, pre-push, and pre-receive, but all hooks work similarly.

  • Key Takeaway: Hooks enhance productivity by enforcing rules automatically, such as checking code before it enters the repo.
  • Link for More Details: Ask AI: Introduction to Git Hooks

Git Workflow and Hook Triggers

In a typical workflow, you write code, stage it, commit locally, and push to a remote repo. Hooks trigger at stages like pre-commit (before local commit), pre-push (before leaving your machine), and pre-receive (on the server before acceptance). This setup allows control over code quality at each step.

Local vs Global Hooks

Local hooks apply only to one repository and live in the .git/hooks folder. Global hooks run across all your repositories by configuring Git to look in a custom directory, like ~/.git/hooks.

  • Key Takeaway: Global hooks are ideal for consistent checks, like secret detection everywhere, while local ones handle project-specific needs.
  • Link for More Details: Ask AI: Local vs Global Hooks

Creating Your First Pre-Commit Hook

Start by running git init to create a .git folder, then navigate to .git/hooks. Rename a .sample file, like pre-commit.sample to pre-commit, to activate it. Hooks are scripts that Git runs automatically on actions.

  • Key Takeaway/Example: The default pre-commit script checks for issues like trailing whitespace. Test by adding a file with spaces and committing—it blocks if errors are found.
# Example from default script
if git rev-parse --verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    against=$(git hash-object -t tree /dev/null)
fi

Handling Permissions and Basic Scripts

New hook files need executable permissions: run chmod u+x .git/hooks/pre-commit. Scripts can be in Bash, Python, or other languages. Start with simple output like echoing "Hello World".

  • Key Takeaway/Example: Without permissions, commits fail with errors. After fixing, a basic hook runs on commit.
#!/bin/sh
echo "Hello World, my first git hook"

Adding Fun Elements: Dad Jokes on Commit

To encourage more commits, add a curl command in your hook to fetch a dad joke from an API.

  • Key Takeaway/Example: This boosts productivity playfully—every commit prints a joke.
#!/bin/sh
curl https://icanhazdadjoke.com

Detecting Secrets in Commits

Use git grep to search commits for patterns like AWS keys (e.g., 20-character alphanumeric strings). If found, echo a warning and exit to block the commit.

  • Key Takeaway/Example: Prevent hard-coding secrets, which stay in Git history forever.
#!/bin/sh
if git grep -E '[A-Z0-9]{20}'; then
    echo "You've hard-coded a secret. No joke for you!"
    exit 1
fi

Integrating Tools like GG Shield

Call external tools like GG Shield in your hook for advanced secret detection: ggshield secret scan pre-commit. It identifies, validates, and advises on secrets.

Setting Up Global Hooks

Create a ~/.git/hooks folder, add a pre-commit file, set permissions, and configure Git: git config --global core.hookspath ~/.git/hooks.

  • Key Takeaway/Example: This applies hooks to all repos without modifying templates.
mkdir ~/.git
mkdir ~/.git/hooks
touch ~/.git/hooks/pre-commit
chmod u+x ~/.git/hooks/pre-commit
git config --global core.hookspath ~/.git/hooks

Combining Global and Local Hooks

In your global hook, add logic to call the local one if it exists: check for .git/hooks/pre-commit and run it with commit data.

  • Key Takeaway/Example: This runs global checks first, then project-specific ones, failing if either does.
#!/bin/sh
local_hook="$GIT_DIR/hooks/pre-commit"
if [ -x "$local_hook" ]; then
    "$local_hook" || exit $?
fi

Advanced Features and Frameworks

Hooks can send reminders, switch branches, or chain actions. For more, use the pre-commit framework—it's language-agnostic, with community hooks for easy configuration.


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: