35

Somehow, git got it in its head that I have an untracked file (A directory that has been in my repository for quite some time, ~17 months). At any rate, I can't seem to convince git that this is not an untracked file. Furthermore, I can't re-add the file(s) in question. Any idea how I can fix this?

Example:

➜ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   rosetta_tests/profile/tests/docking/
nothing added to commit but untracked files present (use "git add" to track)

I try to add the directory and re-check the status.

➜ git add rosetta_tests/profile/tests/docking/
➜ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   rosetta_tests/profile/tests/docking/
nothing added to commit but untracked files present (use "git add" to track)

Just to be sure (from the root of my source tree)

➜ git add .
➜ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   rosetta_tests/profile/tests/docking/
nothing added to commit but untracked files present (use "git add" to track)

What is going on here? How can I convince git to fix itself?

EDIT

A few more details:

-The files are not in the .gitignore

-There are three tracked files in the folder in question, so it's not empty

-After even more digging, it seems something like this could arise from changing the case of a tracked folder. This, unfortunately, is not the case (pun!). The folder name has never changed case.

-Contents of the docking directory:

➜ ls -lA rosetta_tests/profile/tests/docking/.
total 16
-rw-r--r--  1 tim  staff  2209 Jul 17 10:47 command
-rw-r--r--  1 tim  staff   260 Jul 17 10:47 flags
drwxr-xr-x  3 tim  staff   102 Jul 17 10:47 input
Brian
  • 14,610
  • 7
  • 35
  • 43
TimmyJ
  • 928
  • 1
  • 11
  • 22
  • I don't think `.gitignore` is the culprit here. Git-ignored files generally don't show up at all when running `git status`. What I'm more interested in, is if there is anything in that folder? – Frost Jul 17 '12 at 15:05
  • 3
    is the directory empty?, git can't track empty directories. git only tracks files – KurzedMetal Jul 17 '12 at 15:05
  • Should have added these to the original question. The files are not in the .gitignore and there are three tracked files in the folder in question, so it's not empty – TimmyJ Jul 17 '12 at 15:11
  • 1
    Are you experiencing [this issue](http://stackoverflow.com/questions/3492186/git-lies-about-untracked-files?rq=1)? – Justin ᚅᚔᚈᚄᚒᚔ Jul 17 '12 at 15:19
  • No, the case of this directory name has never changed. I tried the mentioned fixes from this post anyways (changing both the index casing and the filesystem casing), neither worked. – TimmyJ Jul 17 '12 at 15:23
  • can you post your .gitignore as well as a dir listing of `rosetta_tests/profile/tests/docking/`? – g19fanatic Jul 17 '12 at 15:32
  • What's the result of `git status --untracked-files` – Peter van der Does Jul 17 '12 at 16:02
  • Do you have a file `rosetta_tests/profile/tests/docking/`? What does `ls -lA rosetta_tests/profile/tests/docking/.` show? – twalberg Jul 17 '12 at 16:31
  • I don't think there are any unprintable characters. See my main post for results of the `ls` command – TimmyJ Jul 17 '12 at 16:54
  • Which git version you're using? If it's msysgit on Windows, I'd bet that the folder case on file system does not match the case in the git index. – Mikko Rantalainen Dec 13 '12 at 11:19

8 Answers8

26

I solved this by running

git clean -f
Alex
  • 2,589
  • 3
  • 35
  • 44
23

The . in the git add . command refers to the "current directory". So, running git add . will add all the files in the current directory and its subdirectories. You should ensure that you are in the correct directory before running git add.

git add -A is what you want. It will add everything.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Th 00 mÄ s
  • 3,776
  • 1
  • 27
  • 46
  • 3
    If he were in the wrong directory, then `git status` wouldn't give him that path to the untracked file. For instance, if he were in a `foo` directory in the same directory as the `rosetta_tests` directory, `git status` would list `../rosetta_tests/profile/tests/docking/`. – Frost Jul 17 '12 at 15:08
  • This is why I mentioned I was in the root of my source tree when running 'git add .' command. – TimmyJ Jul 17 '12 at 15:14
5

I had the same error: no case changes, no .ignore problems and the directory did contain files that I definitely wanted to keep. Adding the dir, committing, git add -A and other remedies did not have any effect.

I ended up deleting the directory with "git clean" and then checking out the files inside the dir again.

Here is what I did:

rex@sidekick> git status
# On branch master
# Your branch is ahead of 'origin/master' by 10 commits.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   app/models/xSpecific/
nothing added to commit but untracked files present (use "git add" to track)

At this point I made a backup copy of the "xSpecific" directory just in case. Then I made a dry run with ´git clean´ (the -n option means "dry run" and -d makes clean remove directories):

rex@sidekick> git clean -n -d
Would remove app/models/xSpecific/

After double and triple checking, I went ahead with the cleaning (the -f flag means "force" and is needed to persuade Git that you are serious. Very good design, if you ask me):

rex@sidekick> git clean -d -f
Removing app/models/xSpecific/

rex@sidekick> git status
# On branch master
# Your branch is ahead of 'origin/master' by 10 commits.
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    app/models/xSpecific/GetAddressesLog.java
#   deleted:    app/models/xSpecific/GetProductLog.java
#
no changes added to commit (use "git add" and/or "git commit -a")

So far, so good. The directory was no longer listed, but the files were of course gone too. I could have restored them from my backup, but this is what Git was made for:

rex@sidekick> git checkout -- app/models/xSpecific/GetAddressesLog.java
rex@sidekick> git checkout -- app/models/xSpecific/GetProductLog.java

rex@sidekick> git status
# On branch master
# Your branch is ahead of 'origin/master' by 10 commits.
#
nothing to commit (working directory clean)

Time to celebrate! Actually, it would probably have been easier to just clone off a new copy of the repository and use that instead, but by doing it the hard way I learned something new :).

Rolf Staflin
  • 2,092
  • 2
  • 22
  • 19
2

This solved the problem for me rm .git/fs_cache

Aurelio
  • 24,702
  • 9
  • 60
  • 63
Robert Childan
  • 983
  • 1
  • 12
  • 22
2

Same issue i got and i solved

git clean -f -n
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
Sikander Bhutto
  • 226
  • 1
  • 11
1

I had important untracked files and couldn't just run git clean -f as some answers suggest.

I try to add the directory and re-check the status.

➜ git add rosetta_tests/profile/tests/docking/

And it seems that adding a whole directory does not trigger a file recheck.

So I tried adding specifically one of the supposedly untracked files and it worked:

$ git status
On branch foo
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        important.txt
        foo/foo.java
        foo/bar.java
        foo/baz.java
        foo/qux.java

nothing added to commit but untracked files present (use "git add" to track)

$ git add -- foo/foo.java
fatal: pathspec 'foo/foo.java' did not match any files

$ git status
On branch foo
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        important.txt

nothing added to commit but untracked files present (use "git add" to track)
kelvin
  • 1,421
  • 13
  • 28
0

This error popped up for me when git add failed due to a file name being too long in a sub directory. (in my case, something in node_modules)

In this case, adding a .gitignore file with content of * to the node_modules folder allowed git add to run to completion.

So, pay attention to the command output of git add . (or -A or --all) and make sure it is not stopping prematurely due to an error. All of my "untracked" files and folders were added successfully once the proper .gitignore was applied and I was able to commit and push my changes.

J E Carter II
  • 1,436
  • 1
  • 22
  • 39
0

Recreate the file or directory

mkdir -p rosetta_tests/profile/tests/docking/

And then remove it again.

rm -rf rosetta_tests/profile/tests/docking/
Edmund Lee
  • 2,514
  • 20
  • 29