Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Hooks

The post-rewrite hook

Reference for the Git post-rewrite hook: invoked by git commit --amend and git rebase, takes amend or rebase as argument, and reads old and new commit pairs on stdin.

post-rewrite runs after Git has replaced commits with rewritten versions. Two commands call it: git commit --amend and git rebase (which includes interactive rebases and git pull --rebase). The hook is told which command ran, and receives a list on standard input that maps every old commit to the commit that replaced it.

It is the hook to use when something outside Git tracks commits by hash: a review tool, a notes mapping, a build cache keyed on commit IDs, or a local database. When the hash changes, the hook can update the mapping. Git itself uses it internally to copy notes (notes.rewriteRef) from old commits to new ones.

Terminal
.git/hooks/post-rewrite <amend|rebase>
# stdin, one line per rewritten commit:
# <old-object-name> <new-object-name> [<extra-info>]

Try it

Bash Amend and rebase

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 Squashing maps many old commits to one

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

Name Description
$1 amend when the rewrite came from git commit --amend, rebase when it came from git rebase.
stdin One line per rewritten commit: <old-object-name> <new-object-name>, optionally followed by a space and extra data (currently unused by Git for these commands).

Exit status: ignored; the rewrite is already complete.

Usage notes

  • For amend, there is exactly one line (or none, if the amend changed nothing that creates a new commit). For rebase, there is one line per commit that was replayed. A squash or fixup produces several lines with different old IDs and the same new ID, which the second example shows.
  • With rebase, the list of all replayed commits arrives in one final call, not after each commit. If the rebase stops for a conflict, that call happens only after you finish with git rebase --continue.
  • Squashing and fixing up inside a rebase make Git amend a commit internally, and each of those amends calls the hook with amend first. The second example shows two amend calls followed by one rebase call. Do not assume that rebase is the only value during a rebase.
  • git cherry-pick, git revert, git filter-branch and git filter-repo do not call it. Tools that rewrite history in bulk need their own bookkeeping.
  • Git's own use: with notes.rewrite.<command> enabled (the default for amend and rebase), notes attached to the old commits are copied to the new ones. This is done by the same mechanism, so your hook runs after the notes are copied.
  • Because the hook knows both IDs, it can print a helpful table, update an issue tracker link, or invalidate caches. Do not assume the old commits are gone: they stay reachable from the reflog until it expires.
  • The hook cannot prevent the rewrite. If you want to forbid it, use pre-rebase.
  • For git pull --rebase, the hook gets rebase as $1, because the rewrite is done by git rebase.