0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git pull時にエラー " Your local changes to the following files would be overwritten by merge"

0
Last updated at Posted at 2026-03-11

実行環境

Amazon Linux

git pull時に以下のエラー

error: Your local changes to the following files would be overwritten 
by merge: 
 .dockerignore 
 Dockerfile 
 config/routes.rb 
 docker-compose.yml 
Please commit your changes or stash them before you merge. Aborting

どんなときに発生?

「git pull で更新(上書き)される予定のファイル」の中に、「まだ保存(commit)も避難(stash)もされていない変更」があるとき

そのまま pull すると上書きされて消える可能性がある

対象ファイル:

  • .dockerignore
  • Dockerfile
  • config/routes.rb
  • docker-compose.yml

git statusを確認

On branch main
Your branch is behind 'origin/main' by 4 commits, 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:   .dockerignore
	modified:   Dockerfile
	modified:   app/assets/builds/.keep
	modified:   config/routes.rb
	modified:   docker-compose.yml
	modified:   public/404.html
	modified:   public/406-unsupported-browser.html
	modified:   public/422.html
	modified:   public/500.html
	modified:   public/flaag.png
	modified:   public/icon.svg
	modified:   public/robots.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	certbot/

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

git status
前回のコミット以降の変化を表示

modified
Gitがすでに管理(追跡)しているファイルに対し「前回の保存時(commit)から中身が変わっている」 という意味

Untracked files
Gitの管理下にまだ入っていない「新しく作られたファイルやフォルダ」

git diffを確認

diff --git a/.dockerignore b/.dockerignore
index cd7190b..eed69f3 100644
--- a/.dockerignore
+++ b/.dockerignore

@@ -46,3 +46,5 @@

 # Ignore Docker-related files
 /.dockerignore
 /Dockerfile*
+
+certbot/

git diff
modifiedで表示されたファイルの
変更の詳細を表示

--- a/.dockerignore
+++ b/.dockerignore
--- ファイルの変更前
+++ ファイルの変更後を表す

--- a/.dockerignore
+++ b/.dockerignore
は
ファイル名は変更されず中身だけ変更されたことを表す

-hello
+hello world
となっていたら
-helloファイルの削除
+hello worldファイルの追加を表す

@@ -46,3 +46,5 @@
46行目あたりが
変更前は3行
変更後は5行になったことを表す

上の例では 空の一行と
certbot/が追加されたという変更

解決方法

これら全ての変更をGitに反映
git add
git commit
その後にもう一度git pull

git pull --rebase

これを

pullしようとしているGitHub(main): A ─ B ─ C ─ D ← 4コミット進んでる
pullで上書きされそうだったローカル: A ─ E ← 変更1コミット

こうする

A ─ B ─ C ─ D ─ E GitHub(main)の内容を反映させた後にローカルの変更を上乗せ

コンフリクト解消

Auto-merging Dockerfile
CONFLICT (content): Merge conflict in Dockerfile

対象ファイル:

  • Dockerfile

1 対象ファイルを編集

vi Dockerfile

2

<<<<<<< HEAD ← GitHub側
=======
>>>>>>> d3f0cce ← ローカル

3 変更の反映

git add Dockerfile

4再度git rebase --continue

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?