83

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?

4
  • 1
    Do you by chance have submodules in use? Perhaps sites/ or sites/default/ or even sites/default/files/? Also check each of those three directories specifically for a .gitignore file. You might also try git add -f sites/default/files/* to add the specific files, rather than adding the directory.
    – twalberg
    Commented Jun 21, 2012 at 16:07
  • nope, no submodules (wasn't aware of them, but will remember them for future use - thanks). Double checked all the directories for more .gitignore files - nothing. And trying to git add -f sites/default/files/* didn't do it: when I touch sites/default/files/test.mine git still doesn't see it.
    – doub1ejack
    Commented Jun 21, 2012 at 18:09
  • 1
    Hmmm... well, are you sure there are no other entries in .gitignore that match then - some wildcard of some sort or something? Or something that would cause the individual files to be ignored - like a *.pyc, and that directory only containing .pyc files? Remember that naming file in .gitignore also matches any subdirectory/file or sub/subdir/file...
    – twalberg
    Commented Jun 21, 2012 at 18:20
  • Arrg! That was exactly it. There another entry of files/ hidden among some other stuff and I was looking for sites/default/... Well that was a lot of effort over a stupid mistake. sigh. thanks.
    – doub1ejack
    Commented Jun 22, 2012 at 14:42

6 Answers 6

84

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.

5
  • So presumably, when I force git to add these files and push them to our development repository, all our other developers will begin tracking them as well right? I'm having a little trouble seeing how that works when their own .gitignore files are ignoring the files being pulled from the dev repo (until they update their local .gitignore files), but I'm ok just trusting git on this one..
    – doub1ejack
    Commented Jun 20, 2012 at 20:35
  • your .gitignore should be in version control. Other developer should get it when they update.
    – J-16 SDiZ
    Commented Jun 20, 2012 at 20:43
  • That's right, they should. However, changes to my .gitignore file didn't solve the problem so I'm left wondering if each of the other developers are going to have to use this same -f fix on their local repos.
    – doub1ejack
    Commented Jun 21, 2012 at 14:38
  • @doub1ejack, when they checkout the .gitignore, they get your /sites/default/files as well. Those file will be tracked correctly.
    – J-16 SDiZ
    Commented Jun 21, 2012 at 14:43
  • When I try update-index, I get an error: fatal: Unable to mark file Commented Jun 8, 2021 at 18:32
13

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?

5
  • Nope, I just have the one .gitignore file and the exclude file is empty.
    – doub1ejack
    Commented Jun 20, 2012 at 20:30
  • Am I missing something then? I've tried to reproduce your situation (pastebin.com/wZB6F8Qj) and after editing .gitignore, the file does show up again as untracked, and git add file works.
    – hdgarrood
    Commented Jun 20, 2012 at 21:04
  • I am starting to wonder as well... after using -f to force git to add a directory if I, again, do a git add git tells me that that the directory is ignored by one of my .gitignore files. I searched my local repository for .gitignore and only found one and opened the exclude file and verified it was not interfering. Am I missing something?
    – doub1ejack
    Commented Jun 21, 2012 at 14:32
  • 1
    Woah... I think I'm out of my depth here. I don't know what's going on, no. This is a bit of a wild stab in the dark, but a fresh checkout in SVN has fixed things for me before... maybe try doing the same with a fresh clone? edit: never mind, just seen your answer
    – hdgarrood
    Commented Jun 27, 2012 at 12:49
  • OMG! Thanks so much for pointing to .git/info/exclude file. My issue was that folder was included there too. +1
    – Kainax
    Commented Nov 11, 2016 at 12:19
11

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.

3

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
2

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/*

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.