Search code examples
gitgit-submodulesgitignoregit-untrackedgit-track

why is git file not tracked in git?


In my git project, the file dir-name/file.txt is not tracked by git, but I want it to be tracked.

  • In git status the file is not listed in the tracked files and not in the untracked files. git add dir-name/file.txt doesn't make any effect.
  • In git ls-files, the dir-name is listed, but dir-name/file.txt doesn't.
  • git check-ignore -v dir-name/file.txt doesn't list anything.
  • git submodule status output: fatal: no submodule mapping found in .gitmodules for path 'dir-name'

How do I get git track this file? I suspect it has something to do with dir-name was a submodule in the past, but I deleted the .submodules file and dir-name/.git/.


Solution

  • I suspect it has something to do with dir-name was a submodule in the past, but I deleted the .submodules file and dir-name/.git/.

    Yes, but those aren't actually how Git tracks submodules. It is not through the .gitmodules file – that's only used to tell Git where to download the submodule from, and it's possible to add submodules without creating any .gitmodules entry.

    (Likewise, the dir-name/.git/ directory was only what caused Git to add dir-name as a submodule entry in the beginning.)

    Instead, the actual "submodule" marker is in the 'tree' (file list) itself: because it was originally added as a submodule, dir-name is not being tracked as a subdirectory at all, it's tracked as a third type of object – running git ls-tree HEAD would show it as a 'commit' entry rather than the usual 'tree' entry.

    You will need to fully remove the entry using git rm --cached dir-name and then re-add it.