CH 35 / 75Part 5: Practical Git Techniques

hooks: Running Scripts Before and After Git Operations

What you will learn on this page

  • Understand that hooks are "scripts that run at fixed points in Git operations"
  • Automatically check contents before a commit with a pre-commit hook
  • Check commit messages with a commit-msg hook
  • Understand that hooks aren't distributed with the repository, and how to share them
On this page8 items

Up to 34, Part 5 has gathered the tools used in real work one by one. The last one is a mechanism for using tools automatically. Mistakes like “I committed with conflict markers still in the file” (Part 4, #24) can be stopped by Git itself rather than by human attention.

hooks are “scripts that run at fixed points”#

A hook is “something you hang things on”. It’s a mechanism for hooking scripts you’ve prepared onto fixed points in operations like git commit and running them.

Hooks live in .git/hooks/, which we saw in Part 3, #20. Samples are there from the start.

text2 lines
cd ~/git-practice
ls .git/hooks
text9 lines
applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-merge-commit.sample
pre-rebase.sample
(and a few more)

If a file exists with the name minus the trailing .sample (such as pre-commit), Git runs it as a hook. The name indicates at which point it runs.

Running git commit: pre-commit checks the contents being recorded, the message is written, commit-msg checks the message, the commit is created, and post-commit runs. If pre-commit or commit-msg fails, the commit stops there
pre-commit and commit-msg stop the commit if they fail
HookWhen it runsIf it fails
pre-commitRight before the commit (before writing the message)The commit stops
commit-msgRight after the message is writtenThe commit stops
post-commitRight after the commit is createdNothing stops (it only notifies)

“Fails” means the script exits with a non-zero exit code.

Checking contents before committing: pre-commit#

Let’s create a pre-commit hook that stops the commit if conflict markers or trailing whitespace remain. It’s one line.

text6 lines
cat > .git/hooks/pre-commit <<'EOF'
#!/bin/sh
# Stop if the changes about to be recorded still contain conflict markers or trailing whitespace
git diff --cached --check
EOF
chmod +x .git/hooks/pre-commit
  • From cat > file <<'EOF' on the first line to EOF is a way of writing the text in between into a file. Creating a file with the same content in an editor works too
  • git diff --cached --check checks the staged changes (Part 2, #10) for conflict markers and trailing whitespace. If there’s a problem, it reports it and exits with failure
  • chmod +x makes the file “executable”. Forget this and the hook won’t run. Git tells you with hint: The '.git/hooks/pre-commit' hook was ignored because it's not set as executable.

As a test, commit with a marker left in.

text2 lines
echo "<<<<<<< HEAD" >> todo.txt
git commit -am "Update the to-do list"
text1 lines
todo.txt:6: leftover conflict marker

The hook reported “a conflict marker remains on line 6”, and no commit was created. Remove the marker and commit again, and it goes through. Since this is practice, we’ll just discard the change.

text1 lines
git restore todo.txt

Checking the message: commit-msg#

The commit-msg hook checks the message you wrote. The message is written to a file, and that file’s location is passed to the hook ($1 inside the script).

Let’s create a hook that stops messages starting with “WIP” (work in progress). The aim is to keep commits meant as work in progress, like the ones we tidied up in Part 4, #26, out of main.

text10 lines
cat > .git/hooks/commit-msg <<'EOF'
#!/bin/sh
# Stop if the message starts with WIP (don't leave commits meant as work in progress)
case "$(head -n 1 "$1")" in
  WIP*|wip*)
    echo "The message starts with WIP. Please write a message that describes the change."
    exit 1 ;;
esac
EOF
chmod +x .git/hooks/commit-msg
text2 lines
echo "Read a book" >> todo.txt
git commit -am "WIP"
text1 lines
The message starts with WIP. Please write a message that describes the change.

It stopped. With a message that describes the change, it goes through.

text1 lines
git commit -am "Add reading to the to-do list"
text2 lines
[main e73a1b3] Add reading to the to-do list
 1 file changed, 1 insertion(+)

Skipping hooks: --no-verify#

When you really don’t want to be stopped just this once, git commit --no-verify skips pre-commit and commit-msg.

Hooks aren’t distributed with the repository#

As explained in Part 3, #20, the contents of .git/hooks/ are not tracked. Even if you commit, they won’t reach anyone else’s machine. This is also a Git safety measure to prevent malicious scripts from being distributed and run without permission.

When a team wants to use the same hooks, change the hooks folder to a tracked folder inside the repository.

text2 lines
mkdir .githooks
git config core.hooksPath .githooks

Hooks placed in .githooks can be committed as ordinary files, and each person can start using them by running git config core.hooksPath .githooks once. However, this means running scripts someone else prepared on your own machine, so read their contents before setting it up.

FAQ#

Q. Do hooks work on Windows too?

A. Yes. Git for Windows includes a mechanism for running scripts that start with #!/bin/sh. If you’re using Git Bash as described in Part 2, #06, you can create them with the same steps as on this page.

Q. Can I write them in something other than shell scripts?

A. Yes. Change what follows #! on the first line and you can write them in Python and so on. Making them executable (chmod +x) is still required.

Summary#

  • hooks are scripts that run at fixed points in Git operations. They live in .git/hooks/
  • The file name indicates when it runs. pre-commit and commit-msg stop the commit if they fail
  • Hook files won’t run unless made executable with chmod +x
  • --no-verify skips them, but using it routinely defeats the purpose of hooks
  • Hooks aren’t distributed with the repository. To share them, use a tracked folder with core.hooksPath

Next page#

36. The Idea of a Remote Repository ── This begins Part 6. We look at the idea of placing another repository outside your local Git. GitHub finally makes its appearance.