git pullをした時の「error: Your local changes to the following files would be overwritten by merge」について

はじめに

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
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.
Updating fc3a58a..12940d1
error: Your local changes to the following files would be overwritten by merge:
        template-parts/tags-list.php
Please commit your changes or stash them before you merge.
Aborting

エラー内容

「ローカルの変更がマージによって上書きされてしまいますよ」というエラー。

git statusでブランチの状態を確認する

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   template-parts/tags-list.php

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

tags-list.phpが修正された状態で、まだステージングされていないことが分かる。

ステージング→コミットを行う

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git add .
hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git commit -m "タグの調整(iMacの作業)"
[master 07b0fee] タグの調整(iMacの作業)
 1 file changed, 10 insertions(+), 5 deletions(-)

プッシュするとrejectされる

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git push
To https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/ki-hi-ro/ki-hi-ro.com-2022.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

現在のブランチの一部が遅れていることが原因。

「git pullをしてください」と書いてある。

git pullをすると特殊なエディタになる

hiroki@shibatahiroshitakanoiMac ki-hi-ro.com-2022 % git pull                                
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
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.
Merge made by the 'recursive' strategy.

こちらはそのエディタを抜けた後の状態。リモートの変更がローカルにマージされた。

デフォルトのメッセージで良ければ、以下のコマンドを入力してvimエディタ(特殊なエディタ)を閉じる。

メッセージを変えたければ、iを押して編集モードにするのがいいだろう。

vimの編集方法については、以下が参考になる(画像をクリックすると別の記事に飛ぶ)

ブランチの状態を視覚的に確認して理解する

VSCodeに以下のような便利コンテンツが備わっていた。

最初にgit pullをした時、リモートで「タグの調整」が行われていたことで、同じファイルが上書きされてしまうため「Your local changes to the following files would be overwritten by merge: template-parts/tags-list.php」というエラーが発生していた。

「タグの調整(iMacの作業)」をステージングして、コミットして、プッシュした時にrejectされたのは、リモートリポジトリの方が進んでいたから。ローカルでは、「記事ページの投稿IDを削除」、リモートでは、「タグの調整」が最新のコミットになっていた。

コミットの進捗状況を同期させるためにgit pullを行った。「Merge branch ‘master’ of https:・・・」でローカルとリモートの進み具合が同期された。再帰的にマージされたということになる。

コメントを残す

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