Git Reference ยท Hooks
The pre-rebase hook
Reference for the Git pre-rebase hook: when it runs, its two arguments, how a non-zero exit aborts the rebase, and how to skip it with --no-verify.
pre-rebase runs at the very start of git rebase, before Git touches anything. If the hook exits with a non-zero status, the rebase is cancelled and your branch stays exactly as it was. That makes it the right place to protect history that other people may already depend on: for example, refusing to rebase a branch that has been published, or a branch that is not yours.
The hook receives the arguments that describe the operation, but not the list of commits. If you need the commits, ask Git yourself with git rev-list.
.git/hooks/pre-rebase <upstream> [<branch>]
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 hook file is named pre-rebase, must be executable, and lives in $GIT_DIR/hooks (or in the directory set by core.hooksPath).
| Name | Description |
|---|---|
$1 (upstream) |
The upstream the rebase was started with, as you typed it (a branch name, tag or commit). |
$2 (branch) |
The branch being rebased. It is only passed when you name the branch on the command line (git rebase main topic); when you rebase the checked-out branch, $2 is empty. |
Exit status: 0 lets the rebase proceed. Any other value aborts it before anything changes. Output on stdout and stderr is shown to the user.
Usage notes
- Because
$2is often missing, read the current branch yourself, for example withgit symbolic-ref --short HEAD, as the second example does. A hook that only looks at$2is bypassed by the most common form of the command. - The hook runs for
git rebase, including interactive rebases andgit pull --rebase(which callsgit rebase). It does not run again for--continue,--skipor--abort. git rebase --no-verifyskips the hook;--verify(the default) lets it run. A hook is a convenience, not a security boundary. A server cannot enforce this rule with a client-side hook.- To find out whether commits are already published, compare with the remote branch:
git rev-list origin/main..HEADlists only local commits. Commits that are reachable from a remote-tracking branch are the dangerous ones to rewrite. - Keep the hook fast. It blocks every rebase, and users who find it annoying will start passing
--no-verify. - When the branch is already based on the upstream and there is nothing to replay, Git reports "up to date" without calling the hook.
- A rebase that is refused leaves no state behind: there is no
.git/rebase-mergedirectory and no detachedHEAD.
Related
- pre-commit, post-rewrite, post-checkout
- git rebase
- Tutorials: Rebasing, Git hooks