Main Branch Commit Protection¶
The pre-tool-use hook denies a Bash command that would commit to the main
or master branch of a repository. It reads the command line to find each
git commit the line runs and the repository each one runs in, and reads
that repository's branch. Text that only mentions committing is not judged.
The protection always runs. No setting or environment variable turns it off,
and --no-verify does not get past it. It is a guard rail against agent
mistakes, not a sandbox: the repository's branch protection on the server is
the real boundary.
Overview¶
The check:
- refuses every
git commitvariant whose repository is onmainormaster; - gives a message that names the branch, the directory whose branch it read, and the workflow to use instead;
- runs before the separate
--no-verifycheck, so--no-verifycannot get a commit past it; - fails open: when a branch cannot be read, that repository is not judged (Fail-open handling);
- allows every other git operation (
push,pull,statusand the rest), also onmainandmaster.
A direct commit to main or master skips code review and can break the
default branch for everyone. The check steers an agent to a feature branch
before the commit is made.
Protected branches¶
mainmaster
Commands that are judged¶
Every git commit, whatever its options, is judged:
git commit -m "message"git commit --amendgit commit --fixup <commit>git commit --no-editgit commit -a -m "message"- any other
git commitvariant, including the forms with git's global options beforecommit(git -C <dir> commit,git --git-dir=<dir> commit)
Commands that are not judged¶
These are allowed on main and master:
git push,git pull,git fetchgit status,git log,git diff,git showgit branch,git checkout,git switchgit merge,git rebase,git cherry-pick- any other git subcommand that is not
commit
git merge, git rebase and git cherry-pick can create commits on the
current branch. The check does not judge them; the repository's branch
protection on the server does.
What counts as a commit¶
The hook splits the command line into simple commands with the shell
tokeniser it shares with the tmux guard
(crates/amplihack-hooks/src/pre_tool_use/shell.rs), and reads each command
by its program word.
| Program word | How the hook reads it |
|---|---|
git whose subcommand, after git's global options, is commit |
A commit. Wrappers before it are skipped, so timeout 5 git commit, env GIT_DIR=… git commit and sudo git commit count. |
sh, bash, zsh, dash, ksh or fish with -c (also -lc and other clusters) |
The operand is read again as a command line, starting from the directory in effect. |
One of those shells with no -c and no script, but with a heredoc of its own |
Each heredoc body is read again as a command line. |
eval |
Its arguments, joined with spaces, are read again as a command line. |
ssh |
The remote command runs on another machine, so no local branch says where it commits. It is not judged. |
python, node, perl, ruby |
A commit started from inside them, for example through Python's subprocess, is not judged, in line with the check's fail-open handling. |
echo, printf, grep, cat, jq, git with a data subcommand (log, show, diff, …) and the other data-only programs |
Their arguments are text. grep -n "git commit" notes.md is allowed. |
| A function defined earlier on the same line whose body only assigns its arguments and prints them with data-only commands | Its arguments are text, as for printf, when a definition is certain to have run before the call and nothing stands before the call but reserved words. A definition that may not have run ((put() { …; }); put …), a body that is not a { …; } group (put() ( … )), a line that runs eval, source or unset, and an assignment or a wrapper before the call (GIT_EDITOR=… put, command put) leave the call judged as any other program. |
Any other program (xargs, find, su, docker, an unknown one) |
A later word git followed by commit is a commit. A later word containing the text git commit is read again as a command line, and when that finds no commit the word itself is judged as one. |
The bodies of the command substitutions the local shell runs ($(…) and
backticks outside single quotes) are read as command lines wherever they
appear, including inside a double-quoted ssh argument. Comments and heredoc
bodies given to data-only programs are not read.
How the repository is found¶
Each commit is judged by the branch of the repository it runs in.
- The base directory. The hook input's
cwd, when it names an existing directory. Otherwise the hook process's working directory. If neither can be read, the branch is read with no directory set, as before. cd. Eachcd(orpushd) to a literal path moves the directory for the commands after it, relative to the directory before it.cdwith no operand,~and~/…useHOME. Acdto a path that is not an existing directory fails, so it leaves the directory as it was.env -Candsudo -D. These options beforegitmove the directory for that command only.git -C <dir>. Each-Capplies in order, relative to the directory the steps above give.-C ""changes nothing.--git-dirandGIT_DIR.--git-dir <dir>,--git-dir=<dir>or aGIT_DIR=<dir>assignment on the git command gives the repository, relative to the directory the steps above give. The branch is read withgit --git-dir=<dir> branch --show-current.
A cd counts only when it is certain to have run in the same shell every time
the commit runs:
cd x && git commit,cd x; git commit,cd xand the commit on separate lines, and(cd x && git commit)judge the commit inx;(cd x); git commit,cd x | true; git commit,cd x & git commit,false && cd x; git commit,cd x || git commit,if …; then cd x; fi; git commitandf() { cd x; }; git commitjudge the commit in both directories, with and without thecd, and deny if either is onmainormaster.
A commit inside a function body runs where the function is called, so it is judged in every directory the line reaches.
Directories the text does not give¶
When the text gives a directory through a variable or another expansion
(cd "$X", git -C "$dir"), as cd -, through popd, as a glob or as
~user, the directory is unresolved, and the commit is judged in the base
directory. A later absolute path replaces an unresolved directory, so
cd "$X" && cd /srv/app && git commit is judged in /srv/app.
The message¶
The deny message gives the directory whose branch was read: the working
directory, the git directory when --git-dir or GIT_DIR gave one, or the
base directory. It never repeats the command.
⛔ Direct commits to 'main' branch are not allowed.
Branch read in: /home/alice/src/app
Commit on a new branch (`git switch -c <name>`) and open a pull request.
This protection cannot be bypassed with --no-verify.
Fallbacks¶
Where the line may run a commit that this reading cannot see, the hook also
judges the base directory when the text contains git commit (after leading
VAR=value assignments and env are removed), as it did before this
reading:
- the quoting or a heredoc is not complete;
- a shell with no
-cand no heredoc of its own, which reads a pipe, a here-string, a redirection or a script file (echo 'git commit -m m' | bash,bash s.sh); source,.,xargsandparallel;- a heredoc whose delimiter is unquoted and whose body contains
$(or a backtick; - more than 64 KiB of input, more than 4 levels of embedded command lines, more than 256 re-reads, or more than 64 commits.
The hook reads at most 16 repositories for one command, and at most 64 cd
commands on one line; past those limits the base directory is judged.
The check fails open for each repository it reads. When git is missing, the
directory is not in a repository, HEAD is detached, or git branch
--show-current takes more than 5 seconds, that repository is not judged.
Limits¶
export GIT_DIR=…or a bareGIT_DIR=…;before the commit is not read. The commit is judged in its working directory.- A
cdinsideeval,sourceor a function call moves the directory for later commands, and the hook does not follow it. - A substitution is judged from the directory its enclosing text starts in,
and from the base directory, not from a
cdearlier on the same line. CDPATHis not read.- Remote runners other than
ssh(mosh,docker exec,kubectl exec) are judged by the local directory. - A word containing
git commitgiven to a program the hook does not know (sed,awk,tee,make) is still judged as a commit. - Only
mainandmasterare protected. A repository whose default branch has another name is not.
Examples¶
From a session whose directory is a clone on main:
| Command | Result |
|---|---|
git commit -m "fix" |
denied |
bash -c 'git commit -m "fix"' |
denied |
git -C ../feature-clone commit -m "fix" (on a feature branch) |
allowed |
cd ../feature-clone && git commit -m "fix" |
allowed |
ssh host 'cd /srv/app && git commit -m "fix"' |
allowed |
grep -n "git commit" notes.md |
allowed |
python3 -c 'print("git commit")' |
allowed |
git push origin main |
allowed: only commits are judged |
From a session on a feature branch, git -C ../main-clone commit -m "fix"
and cd ../main-clone && git commit -m "fix" are denied when main-clone is
on main.
A direct commit to main¶
On main:
Refused:
⛔ Direct commits to 'main' branch are not allowed.
Branch read in: /home/alice/src/app
Commit on a new branch (`git switch -c <name>`) and open a pull request.
This protection cannot be bypassed with --no-verify.
An amend on master¶
On master, git commit --amend --no-edit is refused with the same message,
naming master.
A commit on a feature branch¶
On feature/auth-improvements, git commit -m "feat: add OAuth2 support" is
allowed.
--no-verify on main¶
On main, git commit --no-verify -m "quick fix" is refused by this check,
with the message above. On a feature branch the same command is refused by
the separate --no-verify check instead
(Interaction with the no-verify check).
A push to main¶
git push origin main is allowed: only commits are judged.
Recommended workflow¶
Commit on a branch and open a pull request.
- Create a branch:
Common prefixes: feature/ for new features, fix/ for bug fixes,
docs/ for documentation, refactor/ for refactoring and test/ for
tests.
- Commit on it:
- Push the branch:
- Open a pull request:
- Merge it through the pull request once review and CI have passed.
Configuration¶
There is none. The check always runs, and no setting, environment variable
or .claude file changes it.
The protected branches are fixed in
crates/amplihack-hooks/src/pre_tool_use/git.rs: main and master. A
repository whose default branch has another name (develop, trunk) is not
protected by this check. To protect more branches, see
Adding protected branches.
Fail-open handling¶
The check never refuses a commit because it could not read a branch. Each repository the command reaches is read on its own, and one that cannot be read is not judged; the others still are.
| Situation | Result | What is logged |
|---|---|---|
| The directory is not in a git repository | That repository is not judged | Nothing |
| git is not installed | Not judged | Nothing |
git branch --show-current runs for more than 5 seconds |
git is stopped; not judged | A WARN record, git branch --show-current timed out after 5s, when RUST_LOG lets warnings through |
| Detached HEAD | The branch is empty, so it is not main or master; not judged |
Nothing |
| The hook itself fails (input it cannot read, or a panic) | The hook prints {} and exits 0, so the tool call goes ahead |
A telemetry line with "result":"error" or "result":"panic" |
A detached HEAD is allowed on purpose: cherry-picking, bisecting and reviewing old commits all run there, and so does a commit made before a branch name is chosen.
Under the Codex hook protocol (AMPLIHACK_HOOK_PROTOCOL=codex), a failure of
the hook itself refuses the tool call instead
(Hook Specifications).
Checking logs¶
The hook keeps no log file of its own. It writes to standard error, and where that goes depends on the host:
- one JSON telemetry line for each call, for example
{"hook":"pre_tool_use","duration_us":25693,"result":"ok"}; tracingrecords, as JSON, at the levelRUST_LOGallows (errors only when it is not set).
An earlier version of this page, ported from the Python hook, named
.claude/runtime/logs/pre_tool_use.log and warnings such as
WARNING: Failed to check git branch (not in repo?). The Rust hook writes
neither.
To see what the check decides for a command, run the hook by hand with the
directory the session is in as cwd:
printf '%s\n' '{"hook_event_name":"PreToolUse","tool_name":"Bash","tool_input":{"command":"git commit -m m"},"cwd":"/home/alice/src/app"}' \
| RUST_LOG=warn amplihack-hooks pre-tool-use
The answer is on standard output: {} (or the host's approval) when the
command is allowed, and the refusal with "permissionDecision":"deny" when
it is not.
Troubleshooting¶
I need to make an urgent hotfix to main¶
Do not try to get around the check. Use a hotfix branch:
git switch -c hotfix/critical-issue- Make the fix and commit it.
- Open a pull request and ask for a fast review.
- Merge it through the pull request, or through your team's emergency process.
An urgent fix still gains from review, from CI, and from a record of who changed what and why.
The check does not fire on my default branch¶
Only main and master are protected. If your default branch has another
name, either rename it to main or change the list
(Adding protected branches).
My commit in another clone was refused¶
The message gives the directory whose branch was read (Branch read in:).
If that is not the clone you meant, the command line did not say where the
commit runs in a form the check reads: see
How the repository is found and
Directories the text does not give.
Write the directory literally, with git -C <dir> commit or
cd <dir> && git commit.
A command that only mentions git commit was refused¶
Text given to the data-only programs (echo, printf, grep, cat, jq
and the others) and to python, node, perl and ruby is not judged. A
word holding git commit given to a program the check does not know (sed,
awk, tee, make) is still judged as a commit, and so is a line where the
check falls back to its text test (Fallbacks). Give the text to
a data-only program instead, for example printf '%s\n' '…' >> notes.md.
A refused command is not a commit at all¶
This check only judges commits. Other refusals come from the other checks in
the same hook: --no-verify on a git command
(Interaction with the no-verify check),
removing or moving the session's directory, an unscoped tmux kill, or
tail -f on a device that never ends. The message says which one refused.
Can I turn the check off?¶
No. It is not configurable. If you believe a refusal is wrong, file an issue with the command and the message.
Interaction with the no-verify check¶
A separate check in the same hook refuses --no-verify on git commit,
git push, git rebase, git merge, git cherry-pick and git am, on any
branch. The main-branch check runs first.
The --no-verify check refuses a line that holds --no-verify when either:
- after leading
VAR=valueassignments andenv, the text holdsgit commit,git pushor one of the other subcommands above, as it always has; or - the reading this page describes finds a git command with one of those
subcommands and
--no-verifyamong its own words, also after git's global options and wrappers, insidebash -candeval, and inside a function defined on the line (#3535).
So git -C <dir> commit --no-verify and
timeout 5 git --git-dir=<dir> push --no-verify are refused, where the text
test alone missed them. The text test is kept, so a line refused before is
still refused, including a remote command given to ssh.
| Command | Branch | Main-branch check | --no-verify check |
Result |
|---|---|---|---|---|
git commit -m "msg" |
main |
refuses | not reached | main-branch message |
git commit -m "msg" |
feature/x |
passes | passes | allowed |
git commit --no-verify |
main |
refuses | not reached | main-branch message |
git commit --no-verify |
feature/x |
passes | refuses | --no-verify message |
git -C <feature-clone> commit --no-verify |
any | passes | refuses | --no-verify message |
echo "git -C x commit --no-verify" |
any | passes | passes | allowed |
Implementation¶
crates/amplihack-hooks/src/pre_tool_use/commit_target.rsfinds the commits on a command line and the repository each one runs in, and the git commands that carry--no-verify.crates/amplihack-hooks/src/pre_tool_use/git.rsreads the base directory and each repository's branch, and builds the deny message.crates/amplihack-hooks/src/pre_tool_use/shell.rsis the shared tokeniser, with the reading of subshells, groups, functions, branches and and-or lists that decides whether acdis certain to have run.crates/amplihack-hooks/src/pre_tool_use/mod.rsruns the checks in order and holds the--no-verifycheck.
The reading is larger than a text match. The module documentation of
commit_target.rs, under "Scope", ties each part of it to a requirement of
issue #3535, or to a commit the text test refused that the reading would
otherwise allow, and gives the limits that bound its cost.
This page was first ported from the Python hook (#462), whose files
(.claude/tools/amplihack/hooks/pre_tool_use.py) and log file do not exist in
amplihack-rs. The sections above describe the Rust hook.
Developer reference¶
Execution flow¶
Bash tool call
-> the tmux, tail, XPIA, Skill and working-directory checks
(each may refuse first)
-> main-branch check, when the text holds "commit":
commit_target::find() gives each commit's target and a fallback flag
fallback set and the text holds "git commit"? also judge the base
for each target, at most 16: git branch --show-current, 5 s limit
cannot be read -> skip this target (fail-open)
main or master -> refuse, naming the directory
-> --no-verify check, when the text holds "--no-verify":
the text names git commit/push/rebase/merge/cherry-pick/am,
or commit_target::skips_hooks() finds one with --no-verify
-> refuse
-> allow
Response shape¶
A refused commit gets the shared PreToolUse deny (#1773): block and
message, decision: "block" and reason, a top-level
permissionDecision: "deny" and hookSpecificOutput.permissionDecision:
"deny", each carrying the message below. Claude Code, Copilot and Codex all
act on it. The hook exits 0, so a refusal is information for the agent's next
step, not a failed hook. The check reads the Bash tool; Copilot's bash
tool does not reach it.
Message template¶
MAIN_BRANCH_ERROR in crates/amplihack-hooks/src/pre_tool_use/mod.rs:
⛔ Direct commits to '{branch}' branch are not allowed.
Branch read in: {dir}
Commit on a new branch (`git switch -c <name>`) and open a pull request.
This protection cannot be bypassed with --no-verify.
{dir} is the working directory, the git directory, or the base directory
whose branch was read. When no base directory is known it reads "the hook's
working directory". The message never repeats the command.
Adding protected branches¶
Not configurable. To protect another branch, change the comparison in
check_main_branch in crates/amplihack-hooks/src/pre_tool_use/git.rs:
and add a case to the tests in pre_tool_use_tests.rs.
Security considerations¶
- git runs with a fixed argument list, never through a shell:
git --no-pager branch --show-current, with--git-dir <dir>when the command gave one. - The directories come from the command's text. The command itself would run git in them, so reading their branch adds no access.
- git runs through
amplihack_git, which ignores the hook's inherited git environment (GIT_DIRand the rest) and turns off git's credential prompt.GIT_PAGERandPAGERare set tocat. - git is stopped after 5 seconds.
- The check is one layer. Branch protection on the server, code review and CI are the others.
Performance¶
The reading costs little next to git: each target needs one
git branch --show-current, and a command reaches at most 16 targets. A
command that holds neither commit nor --no-verify is not read at all.
The limits in Fallbacks keep the reading's own cost bounded
for any input.
Testing¶
The tests are in crates/amplihack-hooks/src/pre_tool_use/tests/:
pre_tool_use_tests.rs checks the hook's decisions against two temporary
repositories, one on main and one on a feature branch, and
commit_target_tests.rs checks the directories the reading returns and the
--no-verify reading.
Manual test plan¶
| ID | Command | Branch | Expected |
|---|---|---|---|
| TC1 | git commit -m "test" |
main |
refused, main-branch message |
| TC2 | git commit -m "test" |
master |
refused, main-branch message |
| TC3 | git commit -m "test" |
feature/xyz |
allowed |
| TC4 | git commit --amend |
main |
refused, main-branch message |
| TC5 | git commit --no-verify |
main |
refused, main-branch message |
| TC6 | git commit --no-verify |
feature/xyz |
refused, --no-verify message |
| TC7 | git push |
main |
allowed |
| TC8 | git status |
main |
allowed |
| TC9 | git -C <clone on main> commit -m "test" |
feature/xyz |
refused, names the clone |
| TC10 | cd <clone on a feature branch> && git commit -m "test" |
main |
allowed |
| TC11 | ssh host 'cd /x && git commit -m test' |
main |
allowed |
| TC12 | grep -n "git commit" notes.md |
main |
allowed |
| TC13 | git -C <clone on a feature branch> commit --no-verify |
main |
refused, --no-verify message |
Fail-open tests¶
| ID | Situation | Expected |
|---|---|---|
| EC1 | Not in a git repository | allowed |
| EC2 | git not on PATH |
allowed |
| EC3 | Detached HEAD | allowed |
Changelog¶
- Issue #3535 (December 2026): the check reads the repository each commit
runs in from the command line, judges only
git commitcommands rather than any text containing the words, leaves commits insidesshto the remote machine, and gives the directory whose branch it read in its message. The--no-verifycheck also uses this reading, sogit -C <dir> commit --no-verifyis refused. - First release: commits are denied when the session's own directory is on
mainormaster.
Related documentation¶
- Hook Specifications: the
PreToolUseinput, includingcwd, and the tmux guard that shares the tokeniser. - GitHub branch protection: the repository's settings, for the server-side rule.
.pre-commit-config.yaml: the pre-commit hooks that--no-verifywould skip.