【Git】リモートとローカルの履歴が分岐している場合の対処法3つ

はじめに

git pullできない場合に提示される以下の選択肢3つについて、ここに掲載していこう。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git pull origin main
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 5), reused 6 (delta 4), pack-reused 0 (from 0)
Unpacking objects: 100% (7/7), 726 bytes | 121.00 KiB/s, done.
From https://github.com/ki-hi-ro/ki-hi-ro.com-2022
 * branch            main       -> FETCH_HEAD
   586e053..43ec9cb  main       -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

git config pull.rebase false

この場合は、mergeを行う。

hiroki@shibatahiroshitakanoiMac my-vue-js % git config pull.rebase false
hiroki@shibatahiroshitakanoiMac my-vue-js % 

git pull origin mainを行うと、以下の画面になった。

Merge branch 'main' of https://github.com/ki-hi-ro/my-vue-js
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
~                    

続行をクリックした。

グラフが以下のようになった。

リモートの変更をマージしたということだろう。

上記の画面は、:q → Enterで抜ける。

git push後。

hiroki@shibatahiroshitakanoiMac my-vue-js % git push origin main        
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 12 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 1.72 KiB | 1.72 MiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ki-hi-ro/my-vue-js.git
   ac176f6..08b6851  main -> main

リモートの状態

マージコミットが反映された。

git config pull.rebase true

これは、リモートの変更を先に適応して、その上にローカルの変更を積み重ねる。この設定を行った後にgit pullをすると、ステージングされていない変更があったので、pullできなかった。git stashを行い、CONFLICTを含むgit pullを行うことができた。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git config pull.rebase true
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git pull origin main       
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git stash
Saved working directory and index state WIP on main: d9f2e53 タグ名
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git pull origin main
From https://github.com/ki-hi-ro/ki-hi-ro.com-2022
 * branch            main       -> FETCH_HEAD
Auto-merging template-parts/_tag-names.php
CONFLICT (content): Merge conflict in template-parts/_tag-names.php
error: could not apply d9f2e53... タグ名
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply d9f2e53... タグ名

両方の変更を取り込んだ。

git statusの結果はこちら。コンフリクトが発生して止まっている状態。まだ、gitがローカルとリモートのどちらのファイルを採用するか判断できていない状態。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git status          
interactive rebase in progress; onto 43ec9cb
Last command done (1 command done):
   pick d9f2e53 タグ名
No commands remaining.
You are currently rebasing branch 'main' on '43ec9cb'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   template-parts/_tag-names.php

no changes added to commit (use "git add" and/or "git commit -a")

先ほどの変更をステージに加えて、rebaseを続ける。綺麗なワークツリーになった。

hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git add template-parts/_tag-names.php
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git rebase --continue 
Successfully rebased and updated refs/heads/main.
hiroki@shibatahiroshitakanoMacBook-Air ki-hi-ro.com % git status                           
On branch main
nothing to commit, working tree clean

リベースとは、自分の変更を他のブランチの最新の上に乗せ直す操作。

git config pull.ff only

2025年10月19日、git pullを行うと以下の画面になった。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

ここで、git config pull.ff onlyを設定してみよう。

以下の青がローカルで、紫がリモート。

git pullができなくなってしまった。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git config pull.ff only
hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull               
fatal: Not possible to fast-forward, aborting.

fast-forwardとは、履歴を進めるだけということ。

タグという新しいコミットをローカルで作成してしまっているので、単純に履歴を進めることができない。

一旦、git stashしよう。

と思ったが、コミットした後なので、できなかった。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git stash
No local changes to save

今回は、git config pull rebase false # mergeに設定し直した。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git config pull.rebase false
hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull                    
fatal: Not possible to fast-forward, aborting.

はず、だが、まだfast-forwardの設定が残っていたようだ。

設定を解除して、git pullを行った。

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git config pull.ff false
hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull                
Auto-merging template-parts/_tag-names.php
CONFLICT (content): Merge conflict in template-parts/_tag-names.php
Automatic merge failed; fix conflicts and then commit the result.

コンフリクトを解消して、git add . → git commit → git pushを行うことができた。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です