Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
.git/hooks/pre-rebase <upstream> [<branch>]

Try it

Bash See the arguments Git passes

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 Refuse to rebase a protected branch

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 $2 is often missing, read the current branch yourself, for example with git symbolic-ref --short HEAD, as the second example does. A hook that only looks at $2 is bypassed by the most common form of the command.
  • The hook runs for git rebase, including interactive rebases and git pull --rebase (which calls git rebase). It does not run again for --continue, --skip or --abort.
  • git rebase --no-verify skips 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..HEAD lists 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-merge directory and no detached HEAD.