Bird
Raised Fist0
Gitdevops~10 mins

Creating custom hook scripts in Git - Visual Walkthrough

Choose your learning style10 modes available
Process Flow - Creating custom hook scripts
Start: Git event occurs
↓
Check for hook script in .git/hooks/
Yes↓
Execute hook script
↓
Hook script runs commands
↓
Hook script exits with status
↓
Git continues or aborts based on exit status
No↓
Git continues normal operation
When a Git event happens, Git looks for a hook script in the .git/hooks folder. If found, it runs the script and uses its exit status to decide whether to continue or stop.
Execution Sample
Git
#!/bin/sh
# pre-commit hook example

echo "Running pre-commit checks..."
exit 0
This simple pre-commit hook prints a message and allows the commit to proceed.
Process Table
StepGit EventHook Script Found?ActionHook OutputGit Behavior
1User runs git commitYes (.git/hooks/pre-commit exists)Execute pre-commit hookRunning pre-commit checks...Commit proceeds
2Hook script exits with 0N/AGit continues commitN/ACommit successful
3User runs git commitNoNo hook to runN/ACommit proceeds normally
4User runs git commitYes (hook exists)Execute hookError found, exiting 1Commit aborted
💡 Execution stops after hook script exits; Git uses exit code to continue or abort.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4
Hook Script ExistsUnknownYesYesYes
Hook Exit StatusN/ARunning0 (success)1 (failure)
Git Commit StatusNot startedRunning hookCommit proceedsCommit aborted
Key Moments - 3 Insights
Why does Git abort the commit when the hook script exits with a non-zero status?
Git treats any non-zero exit code from a hook script as a failure signal, so it stops the commit to prevent unwanted changes. See execution_table row 4.
What happens if there is no hook script for a Git event?
Git simply continues the operation normally without running any script, as shown in execution_table row 3.
Can a hook script print messages during execution?
Yes, hook scripts can print messages to inform the user, like in execution_table row 1 where the message 'Running pre-commit checks...' is printed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Git behavior when the pre-commit hook exits with 0?
ACommit proceeds successfully
BCommit is aborted
CGit retries the hook
DGit ignores the hook
💡 Hint
Check execution_table row 2 for hook exit status 0 and Git behavior
At which step does Git abort the commit due to hook failure?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look at execution_table row 4 where hook exits with 1 and Git aborts
If the hook script is missing, what does Git do?
AAborts the operation
BContinues normally
CRuns a default script
DPrompts the user
💡 Hint
See execution_table row 3 for no hook script scenario
Concept Snapshot
Git custom hooks are scripts placed in .git/hooks/ that run on Git events.
If the hook script exists, Git runs it and uses its exit code to decide to continue or stop.
Exit code 0 means success; non-zero aborts the Git operation.
Hooks can print messages to guide users.
No hook means Git proceeds normally.
Full Transcript
When you run a Git command like commit, Git checks if a hook script exists in the .git/hooks folder for that event. If it finds one, it runs the script. The script can do checks or tasks and then exits with a status code. If the exit code is zero, Git continues the operation. If it is non-zero, Git stops the operation to prevent problems. If no hook script is found, Git just continues as usual. Hook scripts can print messages to inform you about what they are doing.

Practice

(1/5)
1. What is the main purpose of creating a custom Git hook script?
easy
A. To delete branches remotely
B. To change the Git commit message manually
C. To create new Git repositories
D. To run scripts automatically at specific Git events

Solution

  1. Step 1: Understand Git hooks

    Git hooks are scripts that run automatically at certain points in Git's workflow, like before a commit or after a push.
  2. Step 2: Identify the purpose of custom hooks

    Custom hooks let you automate tasks or enforce rules during these Git events without manual intervention.
  3. Final Answer:

    To run scripts automatically at specific Git events -> Option D
  4. Quick Check:

    Git hooks = automatic scripts at Git events [OK]
Hint: Hooks run scripts automatically at Git events [OK]
Common Mistakes:
  • Thinking hooks manually change commit messages
  • Confusing hooks with branch deletion
  • Assuming hooks create repositories
2. Which of the following is the correct way to make a Git hook script executable on Linux or macOS?
easy
A. chmod +x .git/hooks/pre-commit
B. git hook enable pre-commit
C. chmod 777 .git/hooks/pre-commit
D. git config hook.pre-commit true

Solution

  1. Step 1: Understand file permissions for hooks

    Git hooks must be executable files to run automatically.
  2. Step 2: Use the correct command to make a file executable

    The command chmod +x adds execute permission without changing other permissions.
  3. Final Answer:

    chmod +x .git/hooks/pre-commit -> Option A
  4. Quick Check:

    Make hook executable = chmod +x [OK]
Hint: Use chmod +x to make hook scripts executable [OK]
Common Mistakes:
  • Using git commands that don't exist for hooks
  • Setting overly permissive permissions like 777
  • Trying to enable hooks via git config
3. Given this pre-commit hook script placed in .git/hooks/pre-commit and made executable:
#!/bin/sh
if git diff --cached --quiet; then
  echo "No changes staged for commit."
  exit 1
fi
What happens when you try to commit without staging any changes?
medium
A. The commit is blocked with message 'No changes staged for commit.'
B. The commit proceeds normally without any message
C. The commit is blocked with a syntax error
D. The commit proceeds but shows a warning

Solution

  1. Step 1: Analyze the script logic

    The script checks if there are no staged changes using git diff --cached --quiet. If true, it prints a message and exits with code 1.
  2. Step 2: Understand Git hook exit codes

    Exiting with code 1 in a pre-commit hook stops the commit process and shows the message.
  3. Final Answer:

    The commit is blocked with message 'No changes staged for commit.' -> Option A
  4. Quick Check:

    Exit 1 in pre-commit = block commit [OK]
Hint: Exit non-zero in pre-commit stops commit [OK]
Common Mistakes:
  • Assuming commit proceeds despite exit 1
  • Confusing staged vs unstaged changes
  • Expecting syntax errors from correct shell script
4. You wrote this post-commit hook script but it does not run after commits:
#!/bin/sh
echo "Commit done!"
Which is the most likely reason?
medium
A. The script filename is incorrect
B. The script is not executable
C. Git hooks cannot echo messages
D. The script must be in the project root, not .git/hooks

Solution

  1. Step 1: Check common hook issues

    Hooks must be executable files inside .git/hooks/ with correct names.
  2. Step 2: Identify the most common cause

    Often, scripts don't run because they lack execute permission, even if named correctly.
  3. Final Answer:

    The script is not executable -> Option B
  4. Quick Check:

    Hooks must be executable to run [OK]
Hint: Make hook scripts executable with chmod +x [OK]
Common Mistakes:
  • Placing scripts outside .git/hooks directory
  • Assuming echo is not allowed in hooks
  • Using wrong hook filenames
5. You want to create a custom Git hook that prevents commits if the commit message does not contain the word 'JIRA'. Which script placed in .git/hooks/commit-msg will achieve this?
hard
A. #!/bin/sh if grep -q 'JIRA' "$1"; then exit 1 fi
B. #!/bin/sh grep 'JIRA' "$1" || echo "Commit message missing JIRA"
C. #!/bin/sh if ! grep -q 'JIRA' "$1"; then echo "Commit message must include 'JIRA'." exit 1 fi
D. #!/bin/sh echo "Checking commit message for JIRA"

Solution

  1. Step 1: Understand commit-msg hook argument

    The commit message file path is passed as the first argument ($1) to the script.
  2. Step 2: Check script logic for enforcing 'JIRA'

    #!/bin/sh if ! grep -q 'JIRA' "$1"; then echo "Commit message must include 'JIRA'." exit 1 fi uses grep -q to check if 'JIRA' is missing, then prints a message and exits with 1 to block the commit.
  3. Step 3: Evaluate other options

    #!/bin/sh grep 'JIRA' "$1" || echo "Commit message missing JIRA" only echoes but does not block commit. #!/bin/sh if grep -q 'JIRA' "$1"; then exit 1 fi blocks if 'JIRA' is present (wrong logic). #!/bin/sh echo "Checking commit message for JIRA" just echoes without checks.
  4. Final Answer:

    #!/bin/sh if ! grep -q 'JIRA' "$1"; then echo "Commit message must include 'JIRA'." exit 1 fi -> Option C
  5. Quick Check:

    Exit 1 if 'JIRA' missing blocks commit [OK]
Hint: Use commit-msg hook with exit 1 to block bad messages [OK]
Common Mistakes:
  • Not exiting with non-zero to block commit
  • Checking for presence instead of absence
  • Only printing message without blocking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard