Trouble un-ignoring previously ignored files in Git repo

GitGitignore

Git Problem Overview


I have a site that has a /sites/default/files/ directory where user content is typically kept. Figuring that I don't need to be tracking this stuff, I added /sites/default/files/ to my .gitignore file.

Then I discovered that I also wanted to keep some mostly-permanent uploads in that directory as well. Now it makes more sense to track everything in /sites/default/files/ and exclude undesirable subdirectories, instead of doing it the other way around.

The problem is that Git won't stop ignoring that directory. I have removed the lines from .gitignore that specify this directory and it won't track it. I have the option to use git add /sites/default/files/ -f to force git to track the directory, but I've done that before and it seems messy.

If .gitignore no longer specifies that directory as ignored, why would I have to force Git to start tracking those files?

Git Solutions


Solution 1 - Git

You know how to do it already. Yes, git add -f is the way.

If you ask why... this is because git set a assume-unchanged bit internally. This bit make git think the file is not modified, so git add won't add it.

If you want to mess with the implementation details, you can use git update-index --no-assume-unchanged <file> command.

Solution 2 - Git

I don't think J-16 is correct. From http://www.kernel.org/pub/software/scm/git/docs/git-add.html:

> The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored. The git add command can be used to add ignored files with the -f (force) option.

git add -f seems to only be for adding a file that does exist in an ignore file somewhere.

Perhaps there is another .gitignore file in /sites or /sites/default which is also telling Git to ignore this dir, or maybe it's been set in .git/info/exclude?

Solution 3 - Git

For posterity: the answer to my problem was "just look closer".

@twalberg pointed out, correctly, that there might be a rule I was overlooking in the .gitignore file. The cascading manner in which the .gitignore rules are applied make it easy to exclude files more broadly than intended.

As far as I can tell, the assumptions I was making about how git works were correct. Similarly, my case doesn't seem to support (or disprove) @J-16 SDiZ 's comments on what role the assume-unchanged bit may play.

Solution 4 - Git

We had a very similar problem in our Drupal sites directory. The issue is that the Drupal distribution has a .gitignore file in the higher level drupal directory that matches anything in drupal/sites/files/*

Solution 5 - Git

In my case, .png and .xlsx files were ignored in the repo. As @doub1ejack, @J-16 SDiZ, and others stated, you need to use git add -f to track the changes. However, git add -f does not work unless you specify the files or patterns explicitly and responds as

Nothing specified, nothing added.

Maybe you wanted to say 'git add .'?

In order to start tracking previously ignored files or patterns:

  1. First, you need to change your working directory to the repository by typing cd <path>.
  2. Second, you need to specify the files or patterns to track them by typing git add -f <file name>.extension for a specific file or git add -f *.xlsx for all excel files.

This unignored the ignored files for my case. You can also check this YouTube video.

Solution 6 - Git

If you mistakenly click on ignored option ion Eclipse git through git staging, then to get back your file do following things:

  1. open your git bash
  2. git status --ignored execute this cmd here your ignored file will displayed
  3. copy the path of that file
  4. git add -f <file path> [add file path after -f ]
  5. git status
  6. file will visible

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondoub1ejackView Question on Stackoverflow
Solution 1 - GitJ-16 SDiZView Answer on Stackoverflow
Solution 2 - GithdgarroodView Answer on Stackoverflow
Solution 3 - Gitdoub1ejackView Answer on Stackoverflow
Solution 4 - GitAl CrowleyView Answer on Stackoverflow
Solution 5 - GitMehmet YildirimView Answer on Stackoverflow
Solution 6 - GitAnkita ThakurView Answer on Stackoverflow