1

Given the following lengthy folder structure in my repo and imagine all these folders has a sub-folder and sub-sub-folder, sub-sub-sub-folder, so on and so on (will only display the first two levels):

jim_folder >
       name.txt
       sub_jim_folder >
                  sub_name.txt 
                  sub_sub_jim_folder >
                          sub_sub_name.txt
                          .....

tim_folder >
       name.txt
       sub_tim_folder >
                  sub_name.txt
                  sub_sub_tim_folder >
                       sub_sub_name.txt
                       ......

amy_folder >
       name.txt
       sub_amy_folder >
                  sub_name.txt
                  sub_sub_amy_folder >
                       sub_sub_name.txt
                       .......
jerry_folder > 
       name.txt
       ........

Basically, I want to only

  • track the jim_folder and all its sub-directories , files within sub-directories and all the sub-sub-directories and all the files within it , so on and so on.
  • And untrack other directories and their sub-directories and every single file or directories under them, i.e. untrack amy_folder, jerry_folder , tim_folder and every single files or directories within them.

Question :

From my understanding, there are 2 approaches to write .gitignore in this situation:

  1. ignore everything in the repo except jim_folder and all its sub-directories and all its sub-sub directories and so on.
  2. ignore explicitly amy_folder, jerry_folder and tim_folder by listing them out.

I would like to see the solution (code) for both approaches

because logically, the first approach should be adopted, but it seems like I would have a lot of lines to write if jim_folder contains many sub, sub-sub, sub-sub-sub, .....directories.

On the other hand, I am not sure how to write the .gitignore file in the second method, do I also need to specify entire structures of those folders that I want to ignore in the .gitignore ? Because that would mean a lot and a lot of lines.

I am sure there is a best practice to this issue. I would like to hear them out.

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96
  • Do you want to track the `name.txt` file under `jim_folder`, too? – mipadi Dec 04 '14 at 01:04
  • Yes. I want to track everything (recursively) in `jim_folder` and untrack everything in the `amy_folder`, `jerry_folder` and `tim_folder`. Everything means all files , sub-directories, sub-sub-directories, sub-subs-sub .... – mynameisJEFF Dec 04 '14 at 01:25

2 Answers2

2

For #1:

*
!.gitignore
!jim_folder
!jim_folder/**/*

For #2:

amy_folder
tim_folder
jerry_folder

jim_folder/**/* matches any file in any subdirectory of jim_folder. You also need to make an exception for jim_folder, apparently. That surprised me, but it makes sense, because jim_folder itself isn't a thing inside jim_folder or its subdirectories, so it doesn't match jim_folder/**/*.

EDIT: it depends on git version - let me know if this works for you.

Community
  • 1
  • 1
fzzfzzfzz
  • 1,248
  • 1
  • 12
  • 22
  • What does `**` mean in here ? And I am using `git version 2.1.3`. After implementing your solution #1, I only see `jim_folder/` in untracked files but stuff like `jim_folder/sub_jim_folder/sub_name.txt` and `jim_folder/sub_jim_folder/sub_sub_jim_folder/sub_sub_name.txt` are still not showing up. – mynameisJEFF Dec 04 '14 at 02:14
  • Try entering `git add jim_folder` - usually git won't list the contents of a directory that isn't tracked in `git status`. For me, when I add `jim_folder`, I get its contents as well. – fzzfzzfzz Dec 04 '14 at 02:32
1

You can do it with this .gitignore pattern:

/*
!/.gitignore
!/jerry_folder
mipadi
  • 398,885
  • 90
  • 523
  • 479