Explore essential concepts of Git Hooks and how they can streamline development workflows with automation. This quiz covers hook types, configurations, usage scenarios, and best practices related to automating tasks with Git Hooks.
Which Git Hook would you use to automatically run a script right before a commit message editor opens to enforce a commit message template?
Explanation: The 'prepare-commit-msg' hook executes just before the commit message editor opens, making it ideal for applying or enforcing commit message templates automatically. 'Pre-push' is triggered before pushing to a remote, not during commit message creation. 'Post-merge' runs after merging changes, unrelated to commits. 'Pre-rebase' triggers before a rebase begins, also unrelated to commit messages.
In a collaborative project, where should Git Hook scripts be placed for them to be recognized and executed by Git during workflow events?
Explanation: Git looks for hook scripts in the .git/hooks directory of the local repository, and only scripts placed here will be executed by Git automatically. The project root or .gitignore location is not recognized by Git for hooks. The global system path may contain templates but is not used for individual repos. Remote server configurations manage access and settings but not local hook execution.
You want to ensure that automated tests run successfully each time before code is pushed to a shared repository. Which Git Hook should be used for this purpose?
Explanation: The 'pre-push' hook is specifically designed to run scripts before a push operation, making it perfect for running and verifying tests before sharing code. 'Post-commit' runs after each commit, not during pushing. 'Pre-applypatch' is seldom used and relates to applying patches, not pushing. 'Post-rewrite' deals with rewriting commits, not test automation before pushes.
After writing a shell script for a pre-commit hook on a Unix-based system, which important step must you perform to ensure the hook is executed by Git when making a commit?
Explanation: Git requires that hook scripts in the .git/hooks directory have executable permissions to run them automatically. Adding the script to an ignore list prevents tracking but does not enable execution. Renaming it with a .txt extension or placing it outside the .git/hooks directory are incorrect, as Git will not recognize or execute the script in those cases.
What is a common practice to help ensure custom Git Hooks are used by all team members, given that hooks are not tracked by version control by default?
Explanation: Since Git does not track the .git/hooks folder, it is common to store hook scripts in a version-controlled directory (like scripts/hooks) and provide setup instructions so other team members can copy them into their .git/hooks directories. There is no .githooksignore file for synchronizing hooks, and .gitconfig is used for configuration settings, not for scripts. Git cannot push files directly to the .git/hooks folder through normal version control operations.