Git Reference ยท Hooks
The post-checkout hook
Reference for the Git post-checkout hook: its three arguments, the branch-or-file flag, when it runs, and uses such as rebuilding dependencies or cleaning up.
post-checkout runs after Git has updated the working tree. It is the hook for chores that depend on which files are now on disk: reinstalling dependencies when a lockfile changed, regenerating a build cache, clearing stale compiled files, or just printing a reminder.
It cannot stop anything, because the checkout is already done. Its exit status becomes the exit status of the command that ran it, so a failing hook makes git checkout or git switch report an error even though the files were switched.
.git/hooks/post-checkout <previous-HEAD> <new-HEAD> <flag>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
The file is called post-checkout and must be executable.
| Name | Description |
|---|---|
$1 |
The full object name of the previous HEAD (the commit you were on). |
$2 |
The full object name of the new HEAD. |
$3 |
A flag: 1 when the whole HEAD changed (a branch checkout, git switch, git checkout <branch>), 0 when only files were retrieved (git checkout <commit> -- <path>). |
Exit status: ignored by Git for the operation itself, but it is passed on as the exit status of the command.
Usage notes
- If
$1equals$2and the flag is1, you did not move to another commit, for example when you create a branch at the current commit (git switch -c new) or switch to a branch that points to the same commit. Hooks that rebuild things can skip that case. - Git also runs the hook after
git clone(when a working tree is checked out) and aftergit worktree add. In both cases the first argument is the all-zero object name because there was no previousHEAD. - The hook runs from the top of the working tree, and
gitcommands inside it work normally. Do not start another checkout from inside it without a guard, or you can create a loop. - Use
git diff --name-only "$1" "$2"to learn which files changed between the two commits. Rebuild only what is affected; running a full install on every switch makes branch switching painful. - Hooks are not copied by
git clone. Share the script through the repository and install it withcore.hooksPath. - Hook output goes to the terminal. Redirect chatty output to a log, or keep it to one line, because it appears on every switch.
git checkout --no-verifydoes not exist:post-checkoutcannot be skipped with a command-line option. Disable it by unsetting the executable bit or pointingcore.hooksPathelsewhere.