0
0
Gitdevops~10 mins

Why hooks automate workflows in Git - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Git hook script that runs before a commit.

Git
#!/bin/sh

[1]
Drag options to blanks, or click blank then click option'
Agit status
Becho "Pre-commit hook running"
Cgit push origin main
Dgit commit -m "message"
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to run git commands inside the hook without context
Not using echo to show output
2fill in blank
medium

Complete the code to make a pre-push hook that prevents pushing if tests fail.

Git
#!/bin/sh

if ! [1]; then
  echo "Tests failed, push aborted."
  exit 1
fi
Drag options to blanks, or click blank then click option'
Agit status
Bls -l
Cecho 'Running tests'
Dmake test
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that don't test code
Not checking command success with if !
3fill in blank
hard

Fix the error in this commit-msg hook that rejects empty commit messages.

Git
#!/bin/sh

if [ -z "$(cat [1])" ]; then
  echo "Empty commit message not allowed."
  exit 1
fi
Drag options to blanks, or click blank then click option'
Acommit-msg
Bmessage.txt
C$1
DCOMMIT_MSG
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding file names
Not using $1 to get the commit message file
4fill in blank
hard

Fill both blanks to create a post-commit hook that logs the commit hash.

Git
#!/bin/sh

commit_hash=$(git [1] -1 --format=%H)
echo "Commit [2]: $commit_hash" >> commit_log.txt
Drag options to blanks, or click blank then click option'
Alog
Bstatus
Chash
Dcommit
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong git commands
Incorrect echo message content
5fill in blank
hard

Fill all three blanks to create a pre-receive hook that rejects pushes to the main branch.

Git
#!/bin/sh

while read oldrev newrev refname; do
  if [ "$refname" = [1] ]; then
    echo "Pushes to [2] are not allowed."
    exit [3]
  fi
 done
Drag options to blanks, or click blank then click option'
A"refs/heads/main"
B"main"
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'main' for refname check
Using exit 0 which allows push