GitでNot possible to fast-forward, aborting.エラーが出た時の対処法

2024.04.18
このエントリーをはてなブックマークに追加

いつも忘れるのでメモ。

ローカルでコミットした後、リモートをプルするときすでにリモートに新しいコミットが作られているとFast forwardが失敗し表題のエラーが発生する。

以下のコマンドでローカルにリモートのコミットを取り込むといい。

git pull --rebase origin main

解説

自分はgit config --global pull.ff onlyでFast forwardのみを許可しているので以下のような時しかRemoteからLocalpullできない。

%%{init: { 'gitGraph': {'mainBranchName': 'Remote'}} }%%
gitGraph
  commit id: "1"
  commit id: "2" tag: "origin"
  branch Local
  checkout Local
  commit id: "3"
  commit id: "4" tag: "main"

以下のような時、

%%{init: { 'gitGraph': {'mainBranchName': 'Remote'}} }%%
gitGraph
  commit id: "1"
  commit id: "2"
  branch Local
  checkout Local
  commit id: "3"
  commit id: "4" tag: "main"
  checkout Remote
  commit id: "5" tag: "origin"

Fast forward onlyだとmergeできない。

%%{init: { 'gitGraph': {'mainBranchName': 'Remote'}} }%%
gitGraph
  commit id: "1"
  commit id: "2"
  branch Local
  checkout Local
  commit id: "3"
  commit id: "4" tag: "main"
  checkout Remote
  commit id: "5" tag: "origin"
  checkout Local
  merge Remote type: REVERSE

rebaseする必要がある。

%%{init: { 'gitGraph': {'mainBranchName': 'Remote'}} }%%
gitGraph
  commit id: "1"
  commit id: "2"
  commit id: "5" tag: "origin"
  branch Local
  checkout Local
  commit id: "3"
  commit id: "4" tag: "main"

参考