What is a Git hook?
A Git hook is a script that Git can execute automatically at specific points in a repository workflow, such as before a commit, after a commit, before a push, or during server-side receive operations.
Git & GitHub interview questions 126-150: Security, Team Workflows, Troubleshooting & Senior Git/GitHub Scenarios.
Open a question for the full answer, code and interview guidance.
A Git hook is a script that Git can execute automatically at specific points in a repository workflow, such as before a commit, after a commit, before a push, or during server-side receive operations.
Client-side hooks run on a developer's machine during operations such as commit or push. They are useful for fast feedback but cannot be trusted as the only enforcement mechanism because users can omit, change, or bypass local hooks.
Commit signing adds a cryptographic signature to a commit so a hosting platform or another user can verify that the commit was signed by a key associated with the claimed signer. Git supports signing mechanisms configured by the developer, including supported GPG, SSH, or S/MIME workflows.
Passwords, API keys, access tokens, private keys, connection secrets, and similar credentials should not be committed to source control. Use appropriate secret stores, environment variables, local configuration excluded by .gitignore, or deployment-platform secret management.
Treat the exposed secret as compromised. Revoke or rotate the credential first so any copied value becomes unusable. Then remove the secret from the current project state and determine whether repository history must be rewritten according to the organization's incident process.
Secret scanning detects supported credential patterns in repository content and can alert repository owners or providers when potentially exposed secrets are found. Available capabilities depend on repository and account configuration.
Dependabot is a GitHub feature that can help identify vulnerable dependencies and automate dependency update workflows. Depending on configuration, it can generate alerts and propose pull requests that update dependency versions.
A merge queue helps repositories integrate pull requests into a busy protected branch while validating them against the latest expected target state. Instead of every approved pull request racing to merge independently, queued changes can be tested in an ordered integration process.
A small team often benefits from a simple strategy: keep main stable, create short-lived feature or fix branches, open focused pull requests, run automated checks, review changes, and delete merged branches.
GitHub Flow is a lightweight branch-based workflow commonly centered on a deployable main branch. Work is performed on a descriptive branch, pushed to GitHub, reviewed through a pull request, validated, merged, and then deployed according to the project's process.
Git Flow is a branching model built around longer-lived branches such as main and develop, with supporting feature, release, and hotfix branches. It was designed for projects with structured release cycles.
Trunk-based development keeps developers integrating frequently into a single primary branch, often called main or trunk. Feature branches, when used, are normally short-lived and changes are kept small.
A hotfix branch is a short-lived branch created to address an urgent production problem. It should contain the smallest safe change needed to correct the issue and should go through appropriate review and testing even when the response is time-sensitive.
First identify the exact production version or commit and understand whether the current main branch can safely produce the fix. If production is maintained on a separate release line, create the fix from the appropriate release commit or branch rather than blindly deploying unrelated main changes.
A force push tells the remote to update a branch even when the update is not a normal fast-forward. It can replace published branch history and make commits used by collaborators disappear from the branch reference.
--force overwrites the remote branch reference without checking whether it changed since your last view. --force-with-lease adds a safety check and normally refuses the update if the remote reference no longer matches the value you expected.
A non-fast-forward rejection usually means the remote branch contains commits that your local branch does not include, or your local history was rewritten. Start by fetching and inspecting the difference rather than immediately force pushing.
First verify the remote URL, repository existence, account access, and whether the repository is private. Then confirm that the selected authentication method is correctly configured and that the credential has the required permissions.
HTTPS and SSH are two common transport and authentication approaches for GitHub Git operations. HTTPS typically authenticates using supported credentials or credential-manager flows, while SSH uses a public/private key pair configured for the account.
Confirm that the branch's required work has been merged, rebased, cherry-picked, tagged, or otherwise preserved. Check the branch commits relative to the target branch and verify that no active pull request or deployment still depends on it.
git fetch --prune fetches updates and removes stale remote-tracking references for branches that no longer exist on the remote. It does not normally delete your local branches.
git branch --contains can list local branches whose history contains a specified commit. Adding appropriate options can include remote-tracking branches.
First synchronize your knowledge of the target branch and inspect the feature branch's commits and diff. Remove accidental files, split unrelated work if necessary, and ensure the branch builds and tests successfully.
Compare the CI environment with the local environment systematically. Check operating system, runtime and SDK versions, environment variables, working directory, file-name casing, line endings, dependency versions, network access, permissions, secrets, and commands used by the workflow.
Keep the primary branch protected and deployable. Developers synchronize with the remote, create short-lived branches for focused changes, make clear commits, and push those branches to GitHub. Changes enter the primary branch through pull requests rather than direct unreviewed pushes.