git runtime_error ai_generated true

error: The following untracked working tree files would be overwritten by checkout: file.txt

ID: git/checkout-conflict-with-untracked-files

Also available as: JSON · Markdown · 中文
85%Fix Rate
90%Confidence
1Evidence
2023-09-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.38.0 active
2.39.0 active
2.40.0 active

Root Cause

Git checkout would overwrite untracked files in the working directory that are present in the target branch but not tracked in the current branch.

generic

中文

Git检出会覆盖工作目录中未跟踪的文件,这些文件在目标分支中存在,但在当前分支中未被跟踪。

Official Documentation

https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt---force

Workarounds

  1. 95% success Move the conflicting untracked files to a safe location: 'mv file.txt file.txt.backup'. Then checkout the branch. After checkout, you can compare and restore if needed.
    Move the conflicting untracked files to a safe location: 'mv file.txt file.txt.backup'. Then checkout the branch. After checkout, you can compare and restore if needed.
  2. 90% success Alternatively, stash the untracked files: 'git stash --include-untracked'. Then checkout the branch. Later apply the stash: 'git stash pop'.
    Alternatively, stash the untracked files: 'git stash --include-untracked'. Then checkout the branch. Later apply the stash: 'git stash pop'.

中文步骤

  1. 将冲突的未跟踪文件移动到安全位置:'mv file.txt file.txt.backup'。然后检出分支。检出后,如果需要可以比较并恢复。
  2. 或者,将未跟踪文件暂存:'git stash --include-untracked'。然后检出分支。之后应用暂存:'git stash pop'。

Dead Ends

Common approaches that don't work:

  1. Adding the file to .gitignore and retrying checkout. 90% fail

    The file is already untracked, so .gitignore won't help; it only prevents future tracking. The checkout still sees the file as a conflict.

  2. Using 'git checkout --force' to override the error. 30% fail

    Force checkout will overwrite the untracked file, potentially losing data. It works but is dangerous.

  3. Running 'git clean -fd' to delete all untracked files blindly. 50% fail

    This deletes all untracked files, not just the conflicting one, which may remove important files.