1

Possible Duplicate:
Git - Whitelisting files in a complex directory structure

I'd like to have a git repository track only files named e.g. SOURCES while everything else shall be ignored (take e.g. a tree of pdf files where each SOURCES file lists their origins). The simplest shot would have been

*
!SOURCES

in .gitignore. However the exclusion of e.g. A/SOURCES is overridden by the *, requiring me to use git add -f. How can .gitignore be modified to ignore everything except files named SOURCES without requiring a forced add?

edit The solution posted here will not do since the directory structure is not fixed, i.e. new directories containing a SOURCES file should not have to be added to .gitignore by hand...

Community
  • 1
  • 1
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • basically also a duplicate of [Make .gitignore ignore everything except a few files](http://stackoverflow.com/q/987142/321973), but that doesn't have the correct answer – Tobias Kienzler Oct 09 '12 at 06:29

1 Answers1

1

You can't achieve this using just .gitignore

Git doesn't track paths. It tracks objects (~ files) only.

So, why don't you reverse the tables:

git add -f -- */*/SOURCES */SOURCES

or

shopt -s globstar
git add -f -- **/SOURCES

Or get out the big guns:

git add -f -- $(find -type f -name SOURCES)

or even

find -type f -name SOURCES -exec git add -f -- {} \+

Untested idea Perhaps something like this could be in a pre-commit hook?


Update An idea for more automation:

Add this to .git/config

[alias]
ac = "!_() { git add -f -- */*/SOURCES && git commit \"$@\"; }; _"

Now, you can just say

git commit -m 'like you usually work'

and it will automatically add the */*/SOURCES

sehe
  • 374,641
  • 47
  • 450
  • 633
  • This will still ignore `A/SOURCES`. Should the order of entries actually matter? – Tobias Kienzler Oct 08 '12 at 15:14
  • @TobiasKienzler you are right. I kind-a misread the question. _And_ the order does not matter. Edited – sehe Oct 08 '12 at 15:18
  • My bad for not being more precise about that... Interesting, I didn't see the connection to git not tracking paths. Well, if I have to use `git add -f` anyway, `.gitignore` can simply contain `*`... I was hoping that `git status` would remind me that I added a new `SOURCES` file somewhere. I could do something really bad/evil/stupid and use a clean/smudge-filter to simply store empty objects for everything that is not a `SOURCES` file... – Tobias Kienzler Oct 08 '12 at 15:23
  • Hm, _if_ I restrict myself to one single level of nesting, the entries `*/*` and `!*/SOURCES` do the trick. However no combination of different depths seems possible... – Tobias Kienzler Oct 08 '12 at 15:30
  • @TobiasKienzler I added a bonus sketch using git alias so you can have the `-f` additions automated. In case you like it – sehe Oct 08 '12 at 16:39
  • 1
    Thanks. Oh, now thanks to [this comment](http://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files#comment11620636_987162) to a related question I realized that `*`, `!*/`, `!SOURCES` actually _does_ work. Making this question a duplicate of [another question](http://stackoverflow.com/q/9162919/321973) though... And: You were actually right, the order in `.gitignore` does matter. Just compare `*`, `!.gitignore` to `!.gitignore`, `*`. Exceptions should apparently be listed last. – Tobias Kienzler Oct 09 '12 at 06:09