CodeBlanks Logo Icon CodeBlanks

update

Learn per-branch checks with the update hook.

What Is the update Hook?

The `update` hook runs separately for each reference update on the server. The script receives the reference name and the old and new object IDs as arguments. This makes it possible to apply different rules to different branches.
#!/bin/bash
refname="$1"
echo "$refname"

Checking Branches with Arguments

The `update` hook receives the reference name as its first argument, the old object ID as its second argument, and the new object ID as its third argument. Branch references begin with `refs/heads/`. The script can reject an update that violates a rule by returning a non-zero exit status.
refname="$1"
if [[ "$refname" == "refs/heads/main" ]]; then
  exit 1
fi

Practice 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

terminal
# Referans adını al
# Dalı denetle

Sample solution

terminal
#!/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

Question
#!/bin/bash
refname="$_____"____
Answer
#!/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

All lessons in Expert