Git Weekly 5 - invisible man

| 3 min read

📋 Summary

A colleague encountered a problem on git that piqued my curiosity. He had a file that was already tracked in git. When he modified it in his IDE, it didn’t list it in the modified files to add to the next commit. And even when editing it in a simple code editor and checking the state of the local repository with the command line, git didn’t see the changes.

How to check which files are ignored by git? #

After checking that the problem didn’t come from the IDE and that we were reproducing the behavior on the command line, we wondered whether the file was being ignored by git. Did you know that it’s possible to list modified files that are ignored by git? To do so, you can add the --ignored option to the git status command, which then adds an “Ignored files” section:

$ git status --ignored
On branch main
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        README.md
        assembly/

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        tmp/

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

It is also possible to use the git ls-files command. This solution has the benefit of listing all files in the case where a folder is ignored[1]. Here’s the exact command to use to list all ignored files:

$ ls-files --exclude-standard -io
tmp/o.tmp
tmp/z.tmp

Be careful not to use the --other option, which also lists modified files already tracked in git :

$ git ls-files --other
.gitignore      
README.md       
assembly/zip.xml
tmp/o.tmp
tmp/z.tmp

Note that there is also the git check-ignore command, which lists ignored files[2] :

$ git check-ignore -- *
tmp

In addition, the -v parameter displays the .gitignore file and the line that causes this exclusion:

$ git check-ignore -v -- *
.gitignore:1:tmp        tmp

Finally, the syntax below allows you to display all files even in the case of an ignored folder:

$ git check-ignore **/*
tmp      
tmp/o.tmp
tmp/z.tmp

Warning: the .gitignore file only allows you to tell git not to list in git status any "untracked " files that are not tracked (as explained here).

Note also that it is possible to have a global .gitignore file in ~/.gitignore or ~/.config/git/ignore[3]. This file may cause unexpected behavior if its existence is forgotten.

How to rebuild the git index for a file? #

All the previous commands have failed to find the file in the ignored list. The next step is to restore the state of the git repository and rebuild the git index for this file (located at assembly/zip.xml) as follows:

git update-index --no-assume-unchanged assembly/zip.xml

How to delete the cache of a given file in git? #

Sometimes, rebuilding the index is not enough to restore a stable and consistent state for a file that git does not recognize as modified. In this case, you need to perform a git reset after deleting the git cache for this file (as explained here):

git rm --cached assembly/zip.xml
git reset assembly/zip.xml

How to force git to add a file to the commit list? #

If, despite all the above operations, git refuses to display the modified file in the list of candidate files for the next commit, then it is still possible to force its hand.
In fact, you can force a file to be added to the next git commit, even if the file is ignored, with the following command :

git add -f assembly/zip.xml

So we’ve just seen a less pleasant side of git[4]. Nevertheless, by searching the documentation and using a few commands, it’s possible to understand what’s going on and repair the state of our local git repository.

Want to learn more about git? Check out the Git Weekly series!


  1. Contrary to the previous command, which only lists the directory. ↩︎

  2. This command only lists the folder and not its contents when ignored. ↩︎

  3. Check with the following command: git config --global --get core.excludesfile. ↩︎

  4. This is not the most dramatic situation, as it can happen that the git index is corrupted, that modifications or commits are lost, or even that a branch is overwritten. These situations are far more stressful and tricky to deal with. But fortunately, that’s not this week’s topic 😅 ↩︎