できない.dev

Git push が rejected(non-fast-forward)で蹴られる

リモートブランチに自分のローカルにない新コミットがあると non-fast-forward で push が拒否される。
fetch → rebase / merge してから push し直すのが基本。

公開: 更新:

症状できない · exit 1
git push origin main
…
error: failed to push some refs to '/tmp/tmp.bLMiob/remote.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
解決後できた · exit 0
git push origin main
To /tmp/tmp.bLMiob/remote.git
   7c73fd0..f402a2d  main -> main

2026-11-15 に alpine/git(Git 2.54.0) で実際に打って取った出力。検証環境の詳細

要約

! [rejected] main -> main (non-fast-forward) のメッセージは リモートブランチが自分のローカルより進んでいることを意味する。
git fetch → rebase または merge してから push し直すのが基本の対応である。

なお、括弧内の理由は状況で変わる。
リモートの先行コミットをまだ取得していない(fetch していない)状態では ! [rejected] main -> main (fetch first) と表示され、すでに取得済みでローカル履歴が分岐している場合に (non-fast-forward) となる。
いずれも fast-forward にならない push が拒否された同じ事象で、対処も同じである。

実行例

alpine/git 環境で上記の手順を動かした。
work-b が先行してリモートを更新した状態で work-a が fetch なしに push を試みると、! [rejected] main -> main (fetch first) が返されて終了コード 1 で停止した。
続く git pull --rebase でリベースが完了した後の push は、終了コード 0 で成功している。

warning: You appear to have cloned an empty repository.
$ git push origin main
To /tmp/tmp.bLMiob/remote.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/tmp/tmp.bLMiob/remote.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ echo $?
1
$ git pull --rebase origin main
From /tmp/tmp.bLMiob/remote
 * branch            main       -> FETCH_HEAD
   eebfbf1..7c73fd0  main       -> origin/main
Successfully rebased and updated refs/heads/main.
$ echo $?
0
$ git push origin main
To /tmp/tmp.bLMiob/remote.git
   7c73fd0..f402a2d  main -> main
$ echo $?
0
$ git log --oneline
f402a2d a の更新
7c73fd0 b の更新
eebfbf1 init

— 2026-11-15 時点の出力

検証環境Git 2.54.0 / 2026-11-15 検証
検証日
実行環境
alpine/git Alpine Linux v3.24
バージョン
  • Git 2.54.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 安全な再同期(共有ブランチで推奨)

git fetch origin
git pull --rebase origin <branch>   # コンフリクト発生時は解消して continue
git push origin <branch>

2. 自分専用ブランチで履歴を上書きしたい場合

git push --force-with-lease origin <branch>

--force-with-lease は リモートが想定通りの状態の時だけ 上書きするため、 他者の新規 push を上書きしてしまう事故を防げる。

3. やってはいけないこと

git push --force origin main   # ❌ 共有ブランチでは絶対に NG

履歴を壊すと、復旧は極めて困難になる。
チームに事故報告と復旧手順が必要になる。

よくある原因

  1. 他者が先に push した。
    共有ブランチに別の人や CI がコミットを足している。
  2. force-push で履歴が書き換えられた。
    元のコミットが消えて、リモートの履歴が分岐している。
  3. fetch が足りていない。
    ローカルが古い HEAD を起点に作業している。
  4. rebase や amend をした。
    ローカル側の履歴が変わり、リモートとの間に差異が出ている。