6.

git「untracked working tree files would be overwritten by

編集
この記事の要点
  • git checkout / switch / pull / merge 時に出るエラー「untracked working tree files would be overwritten
  • 原因: ローカルの未追跡ファイルが、切り替え/取り込み先のブランチに同名で追跡されており、上書きされる危険があるので git が止めた
  • 安全な対処: ① 残したいなら git stash -u で退避 → checkout → git stash pop
  • 不要なら: git clean -fd で削除(取り戻せないので注意)or git checkout -f 強制
  • 予防: 生成物(node_modules.ideadist/ 等)は早めに .gitignore に追加

このエラーの概要

ブランチ切替や pull のときに、こんなメッセージで止まります:

$ git checkout feature/new-ui
error: The following untracked working tree files would be overwritten by checkout:
    src/components/Button.tsx
    src/styles/theme.css
    .env.example
Please move or remove them before you switch branches.
Aborting

# pull でも同様
$ git pull
error: The following untracked working tree files would be overwritten by merge:
    config/local.json
Please move or remove them before you merge.
Aborting

状況を整理すると:

  • あなたのローカルに src/components/Button.tsx という未追跡(git add していない)ファイルがある
  • 切替先のブランチ feature/new-ui には同じパスで別内容の Button.tsx が追跡済み
  • そのまま checkout すると、ローカルの未追跡 Button.tsx が上書きされて消える
  • git は親切に「先に整理してね」と止めてくれている

原因の典型パターン

パターン適切な対処
作りかけのファイルを残したまま切替新しいコンポーネントを書きかけgit stash -u(対処1)
IDE 生成物が .gitignore 未登録.idea/.vscode/.gitignore 追加(対処2)
過去のブランチでコミット忘れ切替前ブランチに add していない戻ってコミット(対処3)
ビルド成果物dist/build/*.pycgit clean -fdX(対処4)
機密ファイル.envcredentials.json退避してから checkout(対処5)

対処1: git stash -u で安全に退避(最も汎用)

未追跡ファイルも含めて退避するには -u オプションが必須です:

# 未追跡含めて退避
git stash push -u -m "before switching to feature/new-ui"

# 古い構文
git stash -u

# 確認
git stash list
# stash@{0}: On main: before switching to feature/new-ui

# ブランチ切替(もうエラーは出ない)
git checkout feature/new-ui

# 戻ってきて pop
git checkout main
git stash pop
# あるいは内容を見るだけ
git stash show -p stash@{0}

# 不要なら破棄
git stash drop stash@{0}

対処2: 該当ファイルだけ別名退避

少数の特定ファイルだけ守りたい場合:

# 移動
mv src/components/Button.tsx /tmp/Button.tsx.bak

# 切替
git checkout feature/new-ui

# 必要なら戻す(diff で確認してから)
diff /tmp/Button.tsx.bak src/components/Button.tsx
# 問題なければ反映
cp /tmp/Button.tsx.bak src/components/Button.tsx

対処3: いったんコミットして切替

WIP(作業中)でも構わないので暫定コミットしてしまうのが最も安全です:

# 新規ブランチを切ってそこにコミット
git checkout -b wip/local-changes
git add .
git commit -m "WIP: local changes before switching"

# 目的のブランチへ
git checkout feature/new-ui

# 後でやり直すなら
git checkout wip/local-changes
# あるいは特定ファイルだけ取り出し
git checkout wip/local-changes -- src/components/Button.tsx

対処4: 不要ファイルを削除して進む

生成物などで残す必要が無いと確信できる場合(戻せないので注意!):

# まずドライランで確認
git clean -nd
# Would remove src/components/Button.tsx
# Would remove dist/

# 実行(-f は force、-d は directory 削除)
git clean -fd

# .gitignore 対象も含めて全消し
git clean -fdx

# .gitignore 対象だけ消す
git clean -fdX

注意: git clean はゴミ箱経由ではなく即削除です。.env など機密設定が消えると復旧不可能なので、必ず -n で事前確認してください。

対処5: checkout -f / pull -f の強制(最終手段)

# 強制 checkout(ローカル変更を捨てる)
git checkout -f feature/new-ui

# git switch の場合
git switch --discard-changes feature/new-ui

# 強制 reset
git reset --hard origin/main

# pull の強制(ローカルを完全に上書き)
git fetch origin
git reset --hard origin/main

これらはローカルの変更を完全に捨てるので、必要なものが含まれていたら復旧できません。

予防: .gitignore で生成物を除外

そもそも未追跡ファイルが大量にあること自体が問題のサイン。生成物は .gitignore に登録しておきます:

# 依存パッケージ
node_modules/
vendor/
.bundle/

# ビルド成果物
dist/
build/
out/
target/
*.pyc
__pycache__/

# IDE 設定
.idea/
.vscode/
*.swp
*.swo
*.iml

# OS ファイル
.DS_Store
Thumbs.db
desktop.ini

# 環境変数 / 認証情報
.env
.env.local
.env.*.local
auth.json
credentials.json
*.pem

# ログ
*.log
logs/

# テストカバレッジ
coverage/
.nyc_output/
htmlcov/

既に commit されてしまった生成物を ignore に追加した場合は:

# 一旦インデックスから外す(ファイルは残る)
git rm -r --cached node_modules
git rm --cached .env

# .gitignore 編集
echo "node_modules/" >> .gitignore
echo ".env" >> .gitignore

git add .gitignore
git commit -m "chore: gitignore build artifacts"

FAQ

Q: git stash -u-u を忘れて元に戻せない
A: 未追跡のままなら現地に残っています。git status で確認してください。stash 自体は追跡変更だけ退避済みです。

Q: 大量のファイルが該当して困る
A: ほぼ間違いなく node_modules / vendor / dist のような生成物。まず .gitignore を整備してから git clean -fdX で除外対象のみ削除すると安全です。

Q: GUI(SourceTree / GitHub Desktop)で同じエラー
A: 同じ原理です。Stash 機能(未追跡含む)を使うか、変更を discard してください。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. fatal: remote origin already exists.
  2. fatal: '~' does not appear to be a git repository
  3. Cannot rebase: You have unstaged changes. Please commit or stash them.
  4. remote: error: denying non-fast-forward refs/heads/master (you should pull first)
  5. error: pathspec ... did not match any file(s) known to git.
  6. The following untracked working tree files would be overwritten by checkout
  7. fatal: Not a valid object name: 'master'.
  8. Unlink of file 'ファイル名' failed. Should I try again? (y/n)
  9. Another git process seems to be running in this repository, ~
  10. error: Your local changes to the following files would be overwritten by checkout: