Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Hooks

The post-merge hook

Reference for the Git post-merge hook: when it runs after git merge or git pull, the squash flag, why it cannot abort a merge, and practical uses.

post-merge runs after git merge has finished successfully, including the merge that git pull performs. By then the working tree has been updated. The hook is typically used to restore things Git does not track but that depend on tracked files: install packages after a lockfile changed, run migrations, or rebuild generated files.

The hook cannot cancel or undo the merge. It does not run when the merge stops with conflicts, because that merge has not completed yet.

Terminal
.git/hooks/post-merge <squash-flag>

Try it

Bash Fast-forward, real merge and squash

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.

Bash No hook run when the merge conflicts

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 named post-merge and must be executable.

Name Description
$1 1 if the merge was a squash merge (git merge --squash), otherwise 0.

Exit status: ignored by git merge for the purpose of the merge, which has already succeeded.

Usage notes

  • The hook receives nothing about which branch was merged. To see what the merge changed, compare ORIG_HEAD with HEAD: git diff --name-only ORIG_HEAD HEAD. Git sets ORIG_HEAD before it merges.
  • That ORIG_HEAD trick is the classic use: if package-lock.json or composer.lock is among the changed files, tell the user to reinstall, or do it for them.
  • A squash merge does not create a commit, so HEAD has not moved when the hook runs. Use the flag to tell the two cases apart; in the squash case compare the working tree against HEAD instead.
  • A conflicted merge never runs the hook. Concluding it later with git commit runs pre-commit and post-commit hooks, but post-merge is not called again.
  • Because git pull uses git merge under the hood, the hook fires after pulls that merge or fast-forward. A pull that uses rebase (pull.rebase) calls post-checkout and post-rewrite instead.
  • The hook runs even for a fast-forward, where no merge commit is made.
  • git merge --no-verify skips only the pre-merge-commit hook, not post-merge.
  • Hooks do not travel with a clone. To share one, keep it in the repository and set core.hooksPath.