3

I'm using Windows 7 with Git v2.8.3.

I have this directory structure which contains a Git repository.

testing_gitignore
│   .gitignore
│   file_1.txt
│   file_2.txt
│
├───dir_1
│       file_11.txt
│       file_12.txt
│       file_13.txt
│
└───dir_2
    │   file_21.txt
    │   file_22.txt
    │   file_23.xlsx
    │   file_24.txt
    │
    └───dir_21
            file_211.txt
            file_212.xlsx
            file_213.txt

I wanna configure the .gitignore file in order to ignore all files inside dir_2 recursively but keeping (for commit) the files with extension: .xlsx (recursively).

Inside .gitignore I used the following:

/dir_2/*
/dir_2/**/*
!/dir_2/*.xlsx
!/dir_2/**/*.xlsx

But I get no success because I get as files to commit the following list (which you can see also here):

.gitignore
file_1.txt
file_2.txt
dir_1\file_11.txt
dir_1\file_12.txt
dir_1\file_13.txt
dir_2\file_23.xlsx

but I expect it should be included (as file to commit) the file:

dir_2/dir_21/file_212.xlsx

Could you give me a .gitignore configuration to achieve this? (before posting here could you try it by yourself with the directory structure I attached before for download on the link?)

David Smith
  • 481
  • 1
  • 5
  • 14

1 Answers1

5

Try to ignore files but re-include folders:

/dir_2/**/*
!/dir_2/**/

(Note the /**/* of the first rule: it ignores all files and folders recursively, then re-include folders !/**/ in the second rule).

Then you can re-include files (because their folders are not ignored)

!/dir_2/**/*.xlsx

The main rule about .gitignore remains:

It is not possible to re-include a file if a parent directory of that file is excluded.

So ignoring files and folders (with '/**/*') means you won't be able re-include files unless their parent folders are themselves re-included (with !/**/: trailing slash means 'folders').
Full .gitignore:

/dir_2/**/*
!/dir_2/**/
!/dir_2/**/*.xlsx

Note that you don't have to commit a .gitignore to test its effect: modify it, and do a git status: as long as those files where not tracked (do a git rm -r --cached dir_2 first), the effect of those rules will be immediate.

With that .gitignore in place, do a git add ., and a git status: it should only show *.xlsx under dir_2/ as being added.

[ pay attention to the underscores ]

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • could you clarify more on this?. I tried your first 3 lines but didn't work. thanks – David Smith Nov 21 '16 at 20:25
  • I tried your last 3 lines but still not working. I haven't make any `commit` yet (even no initial commit), so I think I don't need to do `git rm`. – David Smith Nov 21 '16 at 20:33
  • @DavidSmith What `git status` returns? (also what is your `git version`?) – VonC Nov 21 '16 at 20:34
  • The `git status` returns what you can see on [this image](https://filetea.me/t1s2Lmd0yDVS8u2WxuECK3oxQ/dl). I'm using: `Windows 7` with `Git v2.8.3` – David Smith Nov 21 '16 at 20:38
  • @DavidSmith OK: after testing, I have fixed the answer – VonC Nov 21 '16 at 20:41
  • ups, still not working, I get [this](https://filetea.me/t1s5MH5IaYCSES0AJBthYqSZw/dl). It includes what is strictly inside directory: `dir_2` but not in a recursive manner. What is inside directory: `/dir_2/dir_21/` is not included (I mean `.php` files). – David Smith Nov 21 '16 at 20:50
  • I got a bit close now by doing: `/dir_2/**/*.*` and then `!/dir_2/**/*.xlsx` on the second line. But if I put a file with name: `other` (no extension) inside the directory: `/dir_2/dir_21/` then it will be included on the commit and it suppose not to be because it doesn't have the extension: `.xlsx`. Any idea on how to prevent that inclusion (in a correct way)? – David Smith Nov 21 '16 at 21:17
  • @DavidSmith I have tested the `.gitignore`: it just works: you need to exclude files, then re-include folders. Trying to re-include files won't work fully. In other words, the second line *must* be `/dir_2/**/`. – VonC Nov 21 '16 at 21:27
  • @DavidSmith And try with https://github.com/git-for-windows/git/releases/download/v2.11.0-rc2.windows.1/PortableGit-prerelease-2.11.0.rc2.windows.1-64-bit.7z.exe (it is an autro-extractible archive: no setup). Git 2.8.3 is ancient on Windows. Git 2.11 is more recent. – VonC Nov 21 '16 at 21:30
  • it is working now. You was right with your 3 lines of code above except one small typo I just fixed it. Your suggestion is working for me on Windows 7 / Git v2.8.3 and CentOS v6.8 / Git v2.4.5. You saved my day, thanks a lot! – David Smith Nov 21 '16 at 21:45