update
- Git course
- Expert
- Lesson 7/32
- 4 min
Learn per-branch checks with the update hook.
What Is the update Hook?
#!/bin/bash
refname="$1"
echo "$refname"Checking Branches with Arguments
refname="$1"
if [[ "$refname" == "refs/heads/main" ]]; then
exit 1
fiPractice Reject an Update to the Main Branch
Create an `update` hook script. Assign the reference name from the first argument to the `refname` variable. If the reference is `refs/heads/main`, print an error message and exit with status 1. Allow updates to other branches.
Starter code
# Referans adını al
# Dalı denetleSample solution
#!/bin/bash
refname="$1"
if [[ "$refname" == "refs/heads/main" ]]; then
echo "Ana dal güncellemesi yasak" >&2
exit 1
fi
echo "Güncellemeye izin verildi"Expected output
Güncellemeye izin verildi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the main difference between the update hook and pre-receive?
- It runs after the push operation finishes
- It only runs on the local computer
- It runs separately for each reference update Correct answer
- It only checks file names
Explanation: `update` evaluates each reference update separately, making branch-specific rules easier to implement.
2 Read an update Hook Argument
#!/bin/bash
refname="$_____"____
#!/bin/bash
refname="$_____"1
Explanation: The first argument is accessed with `$1` and contains the reference name in the update hook.
3 Match the update Hook Arguments
| Term | Meaning |
|---|---|
$1 |
Reference name |
$2 |
Old object ID |
$3 |
New object ID |
exit 1 |
Rejects the update |