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.
.git/hooks/post-merge <squash-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 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_HEADwithHEAD:git diff --name-only ORIG_HEAD HEAD. Git setsORIG_HEADbefore it merges. - That
ORIG_HEADtrick is the classic use: ifpackage-lock.jsonorcomposer.lockis among the changed files, tell the user to reinstall, or do it for them. - A squash merge does not create a commit, so
HEADhas not moved when the hook runs. Use the flag to tell the two cases apart; in the squash case compare the working tree againstHEADinstead. - A conflicted merge never runs the hook. Concluding it later with
git commitrunspre-commitandpost-commithooks, butpost-mergeis not called again. - Because
git pullusesgit mergeunder the hood, the hook fires after pulls that merge or fast-forward. A pull that uses rebase (pull.rebase) callspost-checkoutandpost-rewriteinstead. - The hook runs even for a fast-forward, where no merge commit is made.
git merge --no-verifyskips only thepre-merge-commithook, notpost-merge.- Hooks do not travel with a clone. To share one, keep it in the repository and set
core.hooksPath.
Related
- post-checkout, post-rewrite, Hooks overview
- git merge
- Tutorials: Merging, Git hooks