Git Hooks
Objective: The objective of this assignment is to learn how to use Git hooks. Git hooks are scripts that can be triggered by various Git actions, such as commits or pushes. This assignment will help you practice setting up and customizing Git hooks to automate tasks in your Git workflow.
You want to automate certain actions (e.g., running tests, linting) at specific points in the Git workflow using Git hooks.
Instructions:
Part 1: Repository Setup
1. Create a new directory on your computer for this assignment and name it "GitHooksAssignment."
2. Navigate to the "GitHooksAssignment" directory in your terminal or command prompt.
3. Initialize a new Git repository in this directory.
Part 2: Adding a Custom Pre-Commit Hook
1. Create a new file named "pre-commit" in the ".git/hooks" directory of your repository.
2. Open the "pre-commit" file in a text editor.
3. Add a script to the "pre-commit" file that performs a custom task, such as checking for code style violations or running tests. Customize the script to meet your needs.
4. Save and close the "pre-commit" file.
5. Make the "pre-commit" file executable by running the following command:
chmod +x .git/hooks/pre-commit
Part 3: Testing the Custom Pre-Commit Hook
1. Create or modify a file in your repository.
2. Use the following Git command to stage the changes for a commit:
git add <file1> <file2> ...
3. Attempt to commit the changes using the following Git command:
git commit -m "Your commit message here"
- The custom pre-commit hook should run your script and perform the specified task.
Part 4: Adding a Custom Post-Commit Hook (Optional)
1. Create a new file named "post-commit" in the ".git/hooks" directory of your repository.
2. Open the "post-commit" file in a text editor.
3. Add a script to the "post-commit" file that performs a custom task after a commit is made. Customize the script to meet your needs.
4. Save and close the "post-commit" file.
5. Make the "post-commit" file executable by running the following command:
chmod +x .git/hooks/post-commit
Part 5: Testing the Custom Post-Commit Hook (Optional)
1. Commit changes to your repository using the following Git command:
git commit -m "Your commit message here"
- The custom post-commit hook should run your script and perform the specified task.
Part 6: Viewing Git Hooks
1. List the Git hooks available in your repository using the following command:
ls .git/hooks
- This command will display a list of hook files that Git recognizes.
Solutions
Part 1: Repository Setup
# Create a directory
mkdir GitHooksAssignment
# Navigate to the directory
cd GitHooksAssignment
# Initialize a Git repository
git init
Part 2: Adding a Custom Pre-Commit Hook
# Create the pre-commit hook file
touch .git/hooks/pre-commit
# Open the pre-commit file in a text editor and add your script
# Make the pre-commit file executable
chmod +x .git/hooks/pre-commit
Part 3: Testing the Custom Pre-Commit Hook
# Stage changes for a commit
git add <file1> <file2> ...
# Attempt to commit (the pre-commit hook will run)
git commit -m "Your commit message here"
Part 4: Adding a Custom Post-Commit Hook (Optional)
# Create the post-commit hook file
touch .git/hooks/post-commit
# Open the post-commit file in a text editor and add your script
# Make the post-commit file executable
chmod +x .git/hooks/post-commit
Part 5: Testing the Custom Post-Commit Hook (Optional)
# Commit changes (the post-commit hook will run)
git commit -m "Your commit message here"
Part 6: Viewing Git Hooks
# List available Git hooks
ls .git/hooks