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

84,800

Solution 1

Without a complete picture of the repo, what follows is more of a guess than anything else, but it might explain the situation. Let's say your history looks as follows:

A -- C [origin/master]
  \ 
   B [HEAD, master]

You write:

This file has been coming up constantly and when I remove it from file system, git will say I removed this file, while in the other messages, it says it is untracked.

I'm guessing you may have run

git rm --cached <file-in-question>

and committed that deletion in commit B; therefore, the file is no longer tracked in your local repo and but it's still present in your working tree.

In the meantime, the upstream branch received commit C from one of your collaborators, in which <file-in-question> was not removed from version control. What you're attempting to effect with

git pull --rebase

is something like this:

 A -- C [origin/master]
       \ 
        B' [HEAD, master]

However, as the message says,

The [...] untracked working tree [file] would be overwritten by checkout

Indeed, rewinding commit C (in order to replay B on top of it) would result in the revision of <file-in-question> (from commit C) to be checked out in your working tree, in which an untracked file of the same name already exists. The contents of that untracked file may be valuable; you may not want that file to be overwritten by a another version of it. Therefore, Git stops in its track and tells you what's wrong.

Edit: Here is a baby example that reproduces the situation...

cd ~/Desktop
mkdir test
cd test
git init
touch README.md
git add README.md
git commit -m "add README"
# simulate a remote branch moving ahead by one commit
# (that doesn't remove the README)
git checkout -b origin_master
printf "This is a README.\n" > README.md
git add README.md
git commit -m "add description in README"
# remove the README and create a new commit on master
git checkout master
git rm --cached README.md
git commit -m "remove README"
# simulate an attempt to rebase master to its "upstream" branch, origin_master
git rebase --onto origin_master master

That last command spews out the following:

First, rewinding head to replay your work on top of it...
error: The following untracked working tree files would be overwritten by checkout:
    README.md
Please move or remove them before you can switch branches.
Aborting
could not detach HEAD

I suggest you run

git fetch
git log --stat origin/master..master -- <file-in-question>

to check whether something like that happened.

Solution 2

This could also happen due to a case change on the filename. I had the same problem and this is what solved it for me.

git config core.ignorecase true

True for Mac or PC.

Alternative solutions at: The following untracked working tree files would be overwritten by checkout

Solution 3

Remove all untracked files (carefull):

git clean  -d  -fx ""
Share:
84,800

Related videos on Youtube

randomor
Author by

randomor

Just a random dude. I like to create stuff that solves problems, preferably education related.

Updated on July 17, 2020

Comments

  • randomor
    randomor over 2 years

    When I do git status it says nothing to commit, working directory clean

    And then I do git pull --rebase, it says:

    First, rewinding head to replay your work on top of it...
    error: The following untracked working tree files would be overwritten by checkout:
        includes/resources/moduledata/12/_Fr4_02_Invention_IPA_SR_la-Fête.pdf
    Please move or remove them before you can switch branches.
    Aborting
    could not detach HEAD
    

    Similar error when doing git pull origin master

     * branch            master     -> FETCH_HEAD
    error: The following untracked working tree files would be overwritten by merge:
        includes/resources/moduledata/12/_Fr4_02_Invention_IPA_SR_la-Fête.pdf
    Please move or remove them before you can merge.
    Aborting
    

    My .gitignore file:

    → cat .gitignore 
    .htaccess
    bower_components/
    

    This file has been coming up constantly and when I remove it from file system, git will say I removed this file, while in the other messages, it says it is untracked. How could it be untracked and tracked at the same time?

  • randomor
    randomor over 8 years
    Sorry for the late response. I posted the question right before I got off work. Thanks for the extremely detailed answer. But unfortunately the git log doesn't return anything for that file. I'm wondering if it's an encoding error caused by the french character. I copied the file name from log over and it shows up like this: → git log --stat origin/master..master includes/resources/moduledata/12/_Fr4_02_Invention_IPA_SR_la‌​-Fe<0302>te.pdf.
  • jub0bs
    jub0bs over 8 years
    No problem; if you like it, upvote it :) I'm guessing the reason git log doesn't output anything is that accented e. You need to use the actual extended-ASCII chararacter: ê.
  • randomor
    randomor over 8 years
    I never found out what's causing it. My coworker has removed the file from the depository and eventually solved the problem. Thank you for the help.
  • IgorGanapolsky
    IgorGanapolsky about 6 years
    I still get an error: Cannot rebase: Your index contains uncommitted changes. Please commit or stash them.
  • Abhishek Goel
    Abhishek Goel about 6 years
    @IgorGanapolsky this is to remove untracked files not changed files, for changed files you need to do "git stash" and then execute other commands
  • Natan Cox
    Natan Cox over 5 years
    git rm --cached <file-in-question> worked indeed. After some careful pulling, checkouts and merging all was fixed.
  • rocksNwaves
    rocksNwaves about 2 years
    What you are saying happened is exactly what happend to me. I used git rm --cached <File> and now I can't checkout without deleting it. Do you have a solution to this problem?
  • alexy over 1 year
    @AbhishekGoel - I tried this command and it gave me the following error fatal: empty string is not a valid pathspec. please use . instead if you meant to match all paths Changing to git clean -d -fx . worked, thanks

Related