日本語

Fixing “pre-receive hook declined” in Git

​​Git​command line,Troubleshooting

If git push fails with ! [remote rejected] … (pre-receive hook declined), a pre-receive hook on the remote repository rejected the push. The reason the hook gave is on the lines that start with remote: in the same output. Fix the commit or the push target to match that reason, and the push will go through.

This error appears on repositories that enforce commit message rules (such as requiring an issue number) or ban direct pushes to main, using a hook on the remote side.

Note

I tested on Ubuntu 24.04 with Git 2.43.0 and bash 5.2. I ran everything with LANG=C so the messages would be in English. The remote was a local bare repository created in the same environment and registered with the relative path ../remote.git. I put my own pre-receive hook in it. I did not test hooks on hosting services such as GitHub, or how they display results.

A pre-receive hook runs on the remote side

According to the githooks documentation for pre-receive, the pre-receive hook runs on the remote side when it receives a push, just before it updates refs such as branches. It receives one line per ref being updated on standard input, in the form "old commit, new commit, ref name". If the hook exits with a non-zero status, none of the refs are updated. The hook's standard output and standard error are sent back to the side that pushed.

When you run git push locally, the hooks/pre-receive on the remote runs. The hook receives, on standard input, the old commit, the new commit, and the ref name for each ref. If it exits with 0, the branches are updated. If it exits with a non-zero status, no branch is updated. The hook's output appears as remote: lines, and the result appears locally as pre-receive hook declined
The exit status of the pre-receive hook decides whether the remote branches are updated

In my test, I put a hook in the remote's hooks/pre-receive that rejects two things:

  • A direct push to main
  • A commit whose message has no issue number such as #12
bash
#!/bin/bash
# The hooks/pre-receive of the remote (bare repository)
# Each line on stdin is: old commit, new commit, ref name
zero=0000000000000000000000000000000000000000
status=0
while read -r old new ref; do
  if [ "$ref" = "refs/heads/main" ]; then
    echo "main には直接 push できません。別のブランチに push してください。"
    status=1
    continue
  fi
  [ "$new" = "$zero" ] && continue   # Skip branch deletion
  if [ "$old" = "$zero" ]; then
    commits=$(git rev-list "$new" --not --all)   # New branch: commits not yet on the remote
  else
    commits=$(git rev-list "$old..$new")
  fi
  for c in $commits; do
    if ! git log -1 --format=%s "$c" | grep -qE '#[0-9]+'; then
      echo "コミット $(git rev-parse --short "$c") のメッセージに課題番号(#123 など)がありません。"
      status=1
    fi
  done
done
exit $status

The hook lives inside the remote repository. On the pushing side, you can only read the reason on the remote: lines and change your commits or push target so the push is no longer rejected.

When a commit message is rejected

I pushed a commit with the message "Add meow sound" (no issue number) to a new branch, feature/meow. It was stopped with the following message and exit status 1.

console
$ git push origin feature/meow
remote: コミット 2138262 のメッセージに課題番号(#123 など)がありません。        
To ../remote.git
 ! [remote rejected] feature/meow -> feature/meow (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

The text after remote: on the first line is the reason the hook printed with echo. The pre-receive hook declined in parentheses on the [remote rejected] line shows that the pre-receive hook was the one that rejected the push. When I checked the remote branches with git ls-remote --heads origin, feature/meow had not been created.

console
$ git ls-remote --heads origin
ccb463e0b1ce6e784ee405b1be58a9253859fcea	refs/heads/main

Adding –no-verify does not help

Adding git push --no-verify still stopped the push with the same message. As described in the --no-verify section of the git push documentation, this option skips the local pre-push hook. It has no effect on the remote pre-receive hook. I cover the pre-push hook in a separate article, "How to stop a git push when tests fail with a pre-push hook" (in Japanese).

console
$ git push --no-verify origin feature/meow
remote: コミット 2138262 のメッセージに課題番号(#123 など)がありません。        
To ../remote.git
 ! [remote rejected] feature/meow -> feature/meow (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

Fix: if it is the last commit, use git commit –amend

If the rejected commit is the last one, rewrite its message with git commit --amend. The commit hash changed from 2138262 to 1e3d70e, and the same git push then succeeded.

console
$ git commit -q --amend -m '鳴き声を追加 #12'

$ git log --oneline -2
1e3d70e 鳴き声を追加 #12
ccb463e 最初のコミット #1

$ git push origin feature/meow
To ../remote.git
 * [new branch]      feature/meow -> feature/meow

$ git ls-remote --heads origin
1e3d70eab5e09f3cea393f486c16e0faca661394	refs/heads/feature/meow
ccb463e0b1ce6e784ee405b1be58a9253859fcea	refs/heads/main

Fix: if it is an earlier commit, use git rebase -i with reword

Next, I made two more commits and pushed them with the earlier one ("Add purring") missing an issue number. This hook checks each commit being pushed, so it rejects the push even when the last commit has a number. The hash 89820be on the remote: line is the commit to fix.

console
$ git log --oneline -3
4528828 シャーを追加 #12
89820be ゴロゴロを追加
1e3d70e 鳴き声を追加 #12

$ git push origin feature/meow
remote: コミット 89820be のメッセージに課題番号(#123 など)がありません。        
To ../remote.git
 ! [remote rejected] feature/meow -> feature/meow (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

Fix the message of the earlier commit with git rebase -i. To list only the commits that are not yet on the remote, I used the remote-tracking branch origin/feature/meow as the base. In the list that the editor opens, change pick to reword on the line for 89820be and save. Git then opens an editor for that commit's message. In this test, I used sed to make those two editor steps automatically. After rewriting the message, the push succeeded.

console
$ git rebase -i origin/feature/meow
Rebasing (1/2)
[detached HEAD bd096c4] ゴロゴロを追加 #12
 Date: Sat Nov 21 10:00:00 2026 +0900
 1 file changed, 1 insertion(+)
Rebasing (2/2)
Successfully rebased and updated refs/heads/feature/meow.

$ git log --oneline -3
82e72f4 シャーを追加 #12
bd096c4 ゴロゴロを追加 #12
1e3d70e 鳴き声を追加 #12

$ git push origin feature/meow
To ../remote.git
   1e3d70e..82e72f4  feature/meow -> feature/meow

I cover how to rewrite commit order and messages with git rebase -i in a separate article, "How to fix commit order and messages with git rebase -i" (in Japanese).

When a push to main is rejected

I committed on main and ran git push origin main. The hook rejected it with a message saying you cannot push directly to main, and asking you to push to another branch.

console
$ git push origin main
remote: main には直接 push できません。別のブランチに push してください。        
To ../remote.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

A branch pushed together with main is also rejected

I created feature/dog from the commit on main and sent both in one git push. The hook only printed a reason for main, but feature/dog was also rejected with pre-receive hook declined, and neither reached the remote.

console
$ git branch feature/dog

$ git push origin main feature/dog
remote: main には直接 push できません。別のブランチに push してください。        
To ../remote.git
 ! [remote rejected] main -> main (pre-receive hook declined)
 ! [remote rejected] feature/dog -> feature/dog (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

$ git ls-remote --heads origin
82e72f46c027c1b597bf46286d780a83724c34a8	refs/heads/feature/meow
ccb463e0b1ce6e784ee405b1be58a9253859fcea	refs/heads/main

The pre-receive hook runs once per push. If it exits with a non-zero status, none of the refs in that push are updated (githooks documentation for pre-receive). So you may see several [remote rejected] lines but only one reason.

Fix: push to another branch and reset your local main

When I pushed only feature/dog, which carries the commit from main, the push succeeded.

console
$ git push origin feature/dog
To ../remote.git
 * [new branch]      feature/dog -> feature/dog

The commit that could not be pushed to main is still on your local main. The commit is now on feature/dog, so I used git reset --hard origin/main to move the local main back to the same position as the remote.

console
$ git reset -q --hard origin/main

$ git log --oneline -1
ccb463e 最初のコミット #1

$ git ls-remote --heads origin
2c14203bf3a95137fb14fd61fdbb5aa7e47b4dd0	refs/heads/feature/dog
82e72f46c027c1b597bf46286d780a83724c34a8	refs/heads/feature/meow
ccb463e0b1ce6e784ee405b1be58a9253859fcea	refs/heads/main
Warning

git reset --hard also discards uncommitted changes to files tracked by Git. Before you run it, use git status to check whether there are changes you would lose, and make sure the commits you are moving away from exist on another branch (here, feature/dog).

When no remote: lines appear

I replaced the hook with one that exits with exit 1 and prints nothing, then pushed. There were no remote: lines, and only pre-receive hook declined appeared.

console
$ git push origin feature/dog:feature/silent
To ../remote.git
 ! [remote rejected] feature/dog -> feature/silent (pre-receive hook declined)
error: failed to push some refs to '../remote.git'

In this case, the output on the pushing side does not show a reason. Ask the person who manages the remote repository what kinds of pushes it rejects.

Point

When you see pre-receive hook declined, first read the reason the hook printed on the remote: lines, then change your commits or push target to match it.

​Posted by 86