Git and GitHub / Advanced: trust, scale, and recovery / Lesson 15
Repository security, hooks, and credentials
Treat repository automation and configuration as execution boundaries while keeping local convenience separate from enforced server policy.
On this page
Lesson goals
- Explain hook execution and its enforcement limits.
- Protect credentials and inspect trust-sensitive configuration.
A repository can influence executable workflows
Source files are not the only material worth reviewing. Build scripts, dependencies, hooks configured by tooling, submodules, and CI definitions can execute code with the user’s permissions. Cloning does not normally install arbitrary active client hooks from tracked files automatically, but running a project’s setup script may do so. Inspect unfamiliar repositories in an appropriately isolated environment and review commands before execution. A trusted hosting URL does not make every contribution safe. Keep privilege and credentials limited to the task, and do not bypass Git ownership checks by globally trusting every path.
Use a hook as a focused local aid
Git invokes documented hooks at specific lifecycle points, with defined arguments and working-directory context. A pre-commit hook can reject a local commit when a check fails. The example runs a staged whitespace check and returns its result. It is shown as a file body only and is never installed into the workspace. Hook discovery depends on the repository’s configuration and executable-file behavior on the platform. Keep the check fast and deterministic. A hook that examines working files while the commit uses staged content can validate the wrong snapshot, so choose the input state explicitly.
#!/bin/sh
# Synthetic lab hook body; not installed by the course.
exec git diff --cached --check
Save only in an isolated lab for manual execution if practicing. The exit status comes from git diff --cached --check. No hook path, executable permission, or repository configuration is changed in the actual workspace.
Do not rely on client hooks as server enforcement
Client hooks can be absent, changed, or bypassed through documented command options. They improve feedback but cannot independently enforce a shared repository’s policy. Required checks and protected branch or ruleset controls on the hosting service provide a separate enforcement layer, subject to configured bypass roles and permissions. Review that layer explicitly. A local hook success also says only that its own check passed, not that the change is safe. Keep authoritative policy in a trusted reviewed location and make missing evidence visible instead of assuming every contributor ran identical local tooling.
Inspect a narrow configuration question
Configuration origins explain where behavior comes from, but dumping every setting can expose URLs or helper details. Query the relevant key and inspect its scope without publishing secrets. The example asks about hook configuration inside a temporary lab only. A missing key can be normal and does not mean no default hooks directory exists. Credential helpers, signing programs, filters, and merge drivers are additional execution or data boundaries that deserve review. Do not add a global safe.directory wildcard to silence an ownership warning; establish why the repository ownership is unexpected and follow a narrow approved correction.
git config --show-origin --get core.hooksPath
git config --show-origin --get core.autocrlf
These queries may exit 1 with no output when a key is unset; that is not automatically a repository failure. They are inspection examples and do not change configuration. Avoid sharing private paths or credential-related settings in public diagnostics.
Prevent and respond to secret exposure separately
Ignore patterns and local scanning can reduce accidental staging, but neither guarantees that a secret never entered history. Review staged content and keep credentials outside the repository. If exposure occurs, revoke or rotate through the owner’s authorized process before assuming history edits solve it. Copies can remain in clones, caches, pull requests, and artifacts. History cleanup is a coordinated disruptive operation with platform-specific limitations, not a routine reset or file deletion. This course uses no real credentials, performs no secret scanning, and does not rewrite actual history; it teaches the boundary and escalation logic.
Common errors and fixes
A client hook is assumed mandatory for everyone
- Why it happens
- Local feedback is confused with server enforcement.
- How to fix it
- Add trusted hosted controls under the repository policy.
- Prevent it next time
- Test missing and bypassed local-hook scenarios in a lab.
A repository setup script installs unexpected code
- Why it happens
- Automation was executed without reviewing its trust boundary.
- How to fix it
- Stop further execution and inspect scope in an isolated environment.
- Prevent it next time
- Review setup, hooks, and dependencies before running.
Removing a secret file is treated as revocation
- Why it happens
- File visibility is confused with credential validity.
- How to fix it
- Follow authorized revocation and exposure response.
- Prevent it next time
- Keep secrets outside commits and diagnostic artifacts.
Check your understanding
1. A contributor uses --no-verify locally. What should protect the shared branch?
Show worked solution
Trusted required checks and branch or ruleset policy, with reviewed permissions and bypass rules. A client hook can offer quick feedback but cannot guarantee enforcement on someone else’s machine.
2. An unset core.hooksPath query prints nothing. Does that prove hooks are disabled?
Show worked solution
No. Git has a default hooks location when no override is configured. Inspect the documented repository context and files safely. An absent override is different from a disabled mechanism.
Key takeaways
- Repository automation executes with real process capabilities.
- Local hooks assist; trusted server policy enforces collaboration rules.
- Credential response is separate from history visibility.