26

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
randomor
  • 5,329
  • 4
  • 46
  • 68

4 Answers4

39

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

Community
  • 1
  • 1
MikeHall
  • 404
  • 1
  • 4
  • 4
  • This did it for me. I had a messages.properties that I wanted to pull and had a local Messages.properties (dunno why, they were generated). So check if such a file that differs only by upper or lowercase exists and if so execute the above command. – bugybunny Oct 02 '17 at 22:40
  • Thank you it was the case for me too! it solved the problem – Brahim LAMJAGUAR Mar 07 '19 at 13:14
15

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.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 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`. – randomor Aug 20 '14 at 14:17
  • 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: `ê`. – jub0bs Aug 20 '14 at 14:26
  • 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. – randomor Aug 20 '14 at 15:51
  • `git rm --cached ` worked indeed. After some careful pulling, checkouts and merging all was fixed. – Natan Cox Mar 15 '17 at 11:36
  • What you are saying happened is exactly what happend to me. I used `git rm --cached ` and now I can't checkout without deleting it. Do you have a solution to this problem? – rocksNwaves Nov 11 '20 at 17:50
7

Remove all untracked files (carefull):

git clean  -d  -fx ""
Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65
  • I still get an error: `Cannot rebase: Your index contains uncommitted changes. Please commit or stash them.` – IgorGanapolsky Oct 12 '16 at 17:03
  • 1
    @IgorGanapolsky this is to remove untracked files not changed files, for changed files you need to do "git stash" and then execute other commands – Abhishek Goel Oct 13 '16 at 08:21
  • @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 – alexy Jul 28 '21 at 14:09
0

For me this was a version of filename case mismatch, as mentioned by @MikeHall. But in order to fix it I actually needed to amend the most recent commit with the "bad" filenames to their "correct" case. Then after that I was able to successfully rebase beyond that problematic commit.

geogeo
  • 124
  • 2
  • 4