📖 Scenario: You work on a small project where you want to automatically run tests every time you push code to your Git repository. This helps catch errors early and keeps your code healthy.
🎯 Goal: Set up a Git pre-push hook that runs a test script automatically before pushing changes. If tests fail, the push will be stopped.
📋 What You'll Learn
Create a Git hook script file named pre-push inside the .git/hooks directory
Make the pre-push script executable
Write a command inside pre-push to run the test script ./run_tests.sh
Ensure the push is blocked if tests fail (non-zero exit code)
Print a message Tests failed, push aborted. if tests fail
💡 Why This Matters
🌍 Real World
Automated testing on push helps developers catch errors early and maintain code quality in real projects.
💼 Career
Many software teams use Git hooks to automate checks and enforce quality before code is shared with others.
Progress0 / 4 steps
1
Create the Git pre-push hook script file
Create a file named pre-push inside the .git/hooks directory using the command touch .git/hooks/pre-push.
Git
Hint
Use the touch command to create an empty file.
2
Make the pre-push hook script executable
Make the pre-push file executable by running chmod +x .git/hooks/pre-push.
Git
Hint
Use chmod +x to add execute permission.
3
Write the test command inside the pre-push hook
Edit the .git/hooks/pre-push file to add the following lines exactly: #!/bin/sh ./run_tests.sh || { echo "Tests failed, push aborted."; exit 1; }
Git
Hint
Use a shell script that runs ./run_tests.sh and stops push if it fails.
4
Test the pre-push hook by pushing changes
Run git push to test the pre-push hook. If ./run_tests.sh fails, you should see Tests failed, push aborted. and the push should stop.
Git
Hint
Run git push and observe the output message if tests fail.
Practice
(1/5)
1. What is the main purpose of automated testing on push in Git workflows?
easy
A. To run tests automatically when code is pushed to catch errors early
B. To manually review code before merging
C. To delete old branches after pushing
D. To speed up the push command execution
Solution
Step 1: Understand automated testing on push
Automated testing on push means tests run automatically when code is pushed to a repository.
Step 2: Identify the main benefit
This helps catch errors early and keeps the project stable by preventing broken code from merging.
Final Answer:
To run tests automatically when code is pushed to catch errors early -> Option A
Quick Check:
Automated testing on push = run tests automatically [OK]
Hint: Automated testing runs tests on push to catch bugs early [OK]
Common Mistakes:
Thinking it speeds up git push command
Confusing with manual code review
Assuming it deletes branches automatically
2. Which file is commonly used to configure automated tests to run on push in a GitHub repository?
easy
A. package.json
B. README.md
C. .gitignore
D. .github/workflows/ci.yml
Solution
Step 1: Identify configuration file for GitHub Actions
GitHub Actions uses YAML files inside the .github/workflows directory to define workflows.
Step 2: Match file to automated testing on push
The file .github/workflows/ci.yml typically contains instructions to run tests on push events.
Hint: GitHub Actions workflows live in .github/workflows/ [OK]
Common Mistakes:
Choosing .gitignore which ignores files
Selecting README.md which is documentation
Picking package.json which is for Node.js dependencies
3. Given this GitHub Actions workflow snippet, what happens when you push code to the main branch?
name: CI
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: echo "Tests passed!"
medium
A. The workflow fails because 'run' command is invalid
B. The workflow runs only on pull requests, not on push
C. The workflow runs and prints 'Tests passed!' on every push to main
D. Nothing happens because branches is empty
Solution
Step 1: Analyze the trigger event
The workflow triggers on push events to the 'main' branch as specified under 'on: push: branches: [main]'.
Step 2: Check the job steps
The job runs on ubuntu-latest, checks out the code, then runs the command echo "Tests passed!" which prints that message.
Final Answer:
The workflow runs and prints 'Tests passed!' on every push to main -> Option C
Quick Check:
Push to main triggers workflow = prints message [OK]
Hint: Push to main triggers workflow with echo command [OK]
Common Mistakes:
Thinking it runs only on pull requests
Assuming 'run' command is invalid syntax
Believing branches list is empty
4. You wrote this GitHub Actions workflow to run tests on push but it never runs:
name: Test
on:
push:
branch: main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: echo "Running tests"
What is the error?
medium
A. The 'run' command must be inside a 'steps' array
B. The key 'branch' should be 'branches' under 'push' to specify branches list
C. The 'uses' action 'actions/checkout@v3' is deprecated
D. The 'runs-on' value 'ubuntu-latest' is invalid
Solution
Step 1: Check the event trigger syntax
The workflow uses 'branch' instead of 'branches' under 'push'. The correct key is 'branches' which expects a list.
Step 2: Understand impact of wrong key
Because 'branch' is invalid, the workflow never triggers on any push event.
Final Answer:
The key 'branch' should be 'branches' under 'push' to specify branches list -> Option B
Quick Check:
Correct key is 'branches' not 'branch' [OK]
Hint: Use 'branches' (plural) under push, not 'branch' [OK]
Common Mistakes:
Confusing 'branch' with 'branches'
Thinking 'runs-on' value is wrong
Assuming checkout action is deprecated
5. You want to run automated tests only when code is pushed to either the 'main' or 'develop' branches. Which is the correct snippet to add in your GitHub Actions workflow YAML to trigger on these pushes?
hard
A. on:
push:
branches:
- main
- develop
B. on:
push:
branch: ['main', 'develop']
C. on:
push:
branch: 'main, develop'
D. on:
push:
refs: ['main', 'develop']
Solution
Step 1: Understand correct YAML syntax for branches
The 'branches' key expects a list of branch names, each on its own line or in a list format.
Step 2: Compare options
on:
push:
branch: ['main', 'develop'] uses incorrect singular 'branch' key instead of plural 'branches'. on:
push:
branches:
- main
- develop uses the correct 'branches' key with standard YAML list format using dashes, which is widely used.
Step 3: Identify invalid options
on:
push:
branch: 'main, develop' uses 'branch' (wrong key) and a string with comma, which is invalid. on:
push:
refs: ['main', 'develop'] uses 'refs' which is not the correct key for branches under push.
Final Answer:
on:
push:
branches:
- main
- develop -> Option A
Quick Check:
Use 'branches' with list syntax for multiple branches [OK]
Hint: Use 'branches:' with dash list for multiple branches [OK]
Common Mistakes:
Using 'branch' instead of 'branches'
Passing branches as a comma string
Using 'refs' instead of 'branches'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.