Team workflows · lesson 3 of 6
Commands and ideas in this lesson
A.git/hooksscripts Git runs at set moments
Bpre-commit, commit-msgbefore a commit, and to check its message
C--no-verifyskip the hooks
Dpre-commit frameworkhooks shared through the repository

Hooks and checks

Last updated

Lesson 3.4 warned about committing a file with conflict markers still in it. A hook can catch that for you: a small script Git runs at certain moments, which can stop a commit. In this lesson you'll watch a hook stop a bad commit, see how teams share hooks, and why the real guarantee is a check on the host, not on your computer.

By the end of this lesson you can

  • Explain what a Git hook is and when pre-commit and commit-msg run
  • Use the pre-commit framework to share hooks with a team
  • Explain why hooks are a convenience and CI is the guarantee

What a hook is

A hook is a script in your repository's .git/hooks folder, named after a moment. Git runs it at that moment; if it fails, Git stops what it was doing. The two you'll meet most:

  • pre-commit runs before a commit is made. It can look at what's staged and refuse: conflict markers, a file that's too large, code that fails a quick check.
  • commit-msg runs after you write the message. It can refuse a message that doesn't follow the team's rules (lesson 7.2).

Others run before a push (pre-push), after a checkout, and so on. On the host, server-side hooks and checks can refuse a push itself.

A hook stops a bad commit

Kusina now has a pre-commit hook. cat .git/hooks/pre-commit shows it: it looks through the staged changes for conflict markers, and fails if it finds any.

$ git merge more-vinegar
Auto-merging adobo.md
CONFLICT (content): Merge conflict in adobo.md
Automatic merge failed; fix conflicts and then commit the result.
  1. 4d924ec HEAD →main Use coconut vinegar in the adobo
  2. 588df20 more-vinegar Use more vinegar in the adobo
  3. 67e435d Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe

and 2 older commits

1 of 3 The vinegar merge conflicts, as in lesson 3.4.

Now try it, and the escape hatch: git commit --no-verify (or -n) skips pre-commit and commit-msg hooks. git commit --no-verifyCareful: changes files or history, but Git can usually get it back It exists for emergencies, and it's why hooks can't be the last line of defence.

Git sandbox
  1. Read the hook with cat .git/hooks/pre-commit
  2. Merge more-vinegar, stage the file without settling it, and try to commit
  3. Settle the conflict properly and commit

Working folder

no changes

Staging area

nothing staged

Repository

11 commits on main

You are in ~/kusina on main, as in lesson 3.4, but with a pre-commit hook in .git/hooks/pre-commit. more-vinegar still conflicts with main.

Commit graph

  1. 4d924ec HEAD →main Use coconut vinegar in the adobo
  2. 588df20 more-vinegar Use more vinegar in the adobo
  3. 67e435d Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe
  11. 1f359a2 Add sinigang recipe
  12. 601c654 Add adobo recipe

Sharing hooks with a team

.git/hooks is inside .git, so it isn't committed, and git clone doesn't copy it. That's deliberate: a repository you clone can't make your computer run its scripts without you agreeing. So how does a team share hooks?

The usual answer is a framework. With pre-commit (pre-commit.com), the repository has a committed file listing the checks:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: check-merge-conflict
      - id: check-added-large-files
      - id: end-of-file-fixer

Each person runs pre-commit install once, and from then on every commit runs those checks. (rev pins a version of the checks; pre-commit autoupdate moves it to the newest.) Updating the file updates everyone's hooks. Other tools (Husky in JavaScript projects, Lefthook) do the same job.

Hooks are a convenience; CI is the guarantee

Hooks run on each person's computer. Someone may not have installed them, may use --no-verify, or may commit from a tool that skips them. So hooks are a convenience: they catch mistakes early, before you've pushed and waited.

The guarantee is continuous integration (CI): the host runs the same checks on every push and pull request, on its own computers, and a branch rule (lesson 7.4) refuses to merge until they pass. Nobody can skip those. Simple Software Development 7.2 covers CI.

The best setups run the same checks in both places: quickly before each commit, and for certain on the host.

Spot the trap: relying on hooks alone

Spot the trapAna

Ana sets up the conflict-marker hook and tells the family: “Markers can’t get into main any more.”

What happens if…

Check what you learned

When does a pre-commit hook run?
Joaquin clones Kusina. Does he get Ana’s hooks?
Why aren’t hooks enough on their own?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git