9

I want to ignore everything, except a specific subfolder (and all of its contents!). I tried solutions from possible duplicate questions without any success.

I'd need something simple like:

*
!That/Very/Folder/*

But this is not working

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • 1
    Just for curiosity.. why you don't create your repository directly into the subfolder? – Jepessen Jun 15 '17 at 13:01
  • 1
    From the [documentation of gitignore](https://git-scm.com/docs/gitignore): *It is not possible to re-include a file if a parent directory of that file is excluded.* In your case `That` is excluded by the match-all (`*`) ignore pattern, and therefore anything below `That` cannot be re-included. – Leon Jun 15 '17 at 13:02
  • 1
    Anyway you can try to add `!*/`. Can you provide a sample of your output of `git status`? – Jepessen Jun 15 '17 at 13:02
  • Possible duplicate of [.gitignore exclude folder but include specific subfolder](https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder) – Leon Jun 15 '17 at 13:05
  • 2
    Possible duplicate of [How do I tell Git to ignore everything except a subdirectory?](https://stackoverflow.com/questions/1248570/how-do-i-tell-git-to-ignore-everything-except-a-subdirectory) – TriskalJM Jun 15 '17 at 13:07
  • @Jepessen I want to maintain my library submodules this way (in any project folder). Unfortunately I have to have to maintan library files scattered around in numerous subfolders, while the root folder is mostly occupied by the actual project itself. – Geri Borbás Jun 15 '17 at 13:08
  • @Jepessen Wow, `!*/` seems solved my issue! Much thanks! – Geri Borbás Jun 15 '17 at 13:09
  • Glad to hear it. It allows to unignore also what's present in subdirectories. – Jepessen Jun 15 '17 at 13:17
  • Does this answer your question? [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – JFFIGK Dec 17 '19 at 21:31
  • Nope, but I was there too. For that question, you just need to commit the deletion of the file. – Geri Borbás Dec 17 '19 at 23:39

3 Answers3

21

Your .gitignore almost works but it doesn't for a simple reason: the first rule (*) tells Git to ignore every file and directory in the root of the repository. Git honors it and ignores everything, including the That directory and its content. The "unignore" rules that follow do not match anything inside the That subdirectory because the That directory is ignored together with it content, and they don't have effect.

In order to tell Git to not ignore files and directories in a deeply nested sub-directory you have to write ignore and unignore rules to let it reach the enclosing sub-directory first and then add the rules you want.

Your .gitignore file should look like this:

### Ignore everything ###
*

# But do not ignore "That" because we need something from its internals...
!That/

# ... but ignore (almost all) the content of "That"...
That/*
# ... however, do not ignore "That/Very" because we need to dig more into it
!That/Very/

# ... but we don't care about most of the content of "That/Very"
That/Very/*
# ... except for "That/Very/Folder" we care
!That/Very/Folder/
# ... and its content
!That/Very/Folder/*
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thanks or the clarifications! This simple addition however, https://stackoverflow.com/questions/44568184/gitignore-everything-except-a-subfolder-with-every-content#comment76124976_44568184 seems make it work. – Geri Borbás Jun 15 '17 at 13:34
  • Wow, I love this answer. Unfortunately, I can't make it work for my case "deeply nested file". Can you take a look? https://stackoverflow.com/q/56222119/6141587 – deadcoder0904 May 21 '19 at 05:05
  • @deadcoder0904 Read this answer again carefully then try to apply it to your situation. Pay attention to the details. – axiac May 21 '19 at 05:09
  • @axiac I managed to make it work. thank you but just have one gripe. I don't understand how to add files now. the last step adds sub-folder but I want only one file from that subfolder. also, my `.gitignore` has become too big trying to accommodate that deeply nested folder. is it normal? – deadcoder0904 May 21 '19 at 05:20
18
*
!*/
!That/Very/Folder/**
!Also/This/Another/Folder/**

Ignore everything, allow subfolders (!), then allow specific folder contents (with unlimited subfolders within).

Credits to @Jepessen for the middle piece that makes it work.

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
3

I want to ignore everything

add the folder to the gitignore

except a specific subfolder (and all of its contents!).

force folder to be added to the repository

git add -f folder

EDIT:

I use this solution for example when I need to keep log folder, for example, but not its content. Generally, when I suppose the content of the folder is never to be added. And generally I add just path/to/folder/.gitkeep file with -f option.

sensorario
  • 20,262
  • 30
  • 97
  • 159
  • This solution works for the moment but it doesn't convince Git to care about the new files created later in that folder. The folder and its content is still ignored. – axiac Jun 15 '17 at 13:34
  • Doesn't it? I did not actually try this (as I like maintaining the rules explicitly written better). – Geri Borbás Jun 15 '17 at 13:40