1

i use git to manage my sources,i have some file in bellow paths:

Debug/a.dll
Debug/b.exe
Debug/c.png
Debug/Pic/a.png
Debug/Pic/b.bmp
Debug/Pic/c.dll

I want to ignore those files:

Debug/a.dll
Debug/b.exe
Debug/c.png

But to excelude those files from ignore:

Debug/Pic/a.png
Debug/Pic/b.bmp
Debug/Pic/a.dll
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Kūrosh
  • 442
  • 1
  • 5
  • 22
  • If you want to understand why codeWizard's answer (which was false initially, before he added the missing element from my answer) work, see [my answer below](http://stackoverflow.com/a/30606460/6309) – VonC Jun 06 '15 at 07:11

3 Answers3

2

Git use the .gitignore to ignore or to track files in ignored paths.

In your case you need to do add this to your .gitignore file in your project root directory. Create the file is it does not exist

#List of files to ignore
Debug/*

#List of files to exclude from the ignored pattern above
!Debug/Pic
!Debug/Pic/*

What is in content of this sample .gitignore

Debug/* - This will ignore all the files under the Debug folder
!Debug/Pic/* - The ! is a special character in this case telling git to exclude the given pattern from the ignored paths.

In other words:
We "told" git to ignore all the files under the Debug folder but to include all the files under the Debug/Pic/ folder.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
2

As usual, with exclusion in gitignore, the rule to remember is:

It is not possible to re-include a file if a parent directory of that file is excluded (*)
(*: unless certain conditions are met in git 2.?, see below)

Since Debug/* would ignore the folder Debug/Pic/, trying to exclude the content of that folder would not work (!Debug/Pic/*) with git 2.6 or less.

You need to exclude the folder first. Then its content.

Debug/*
!Debug/Pic/
!Debug/Pic/*

Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

So in git 2.9+, in order to re-include the the folder Debug/Pic/, you can do:

/Debug
!Debug/Pic
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You can add the subdirectory first, then ignore the containing directory:

git add Debug/Pic
git ignore Debug

This will have the side effect of not showing the addition of new files to Debug/Pic, but you can just add them manually with 'git add -f' to get around the .gitignore warning.

Michael Chinen
  • 17,737
  • 5
  • 33
  • 45
  • It's part of the git-extras suite http://stackoverflow.com/questions/1677121/is-there-an-ignore-command-for-git-like-there-is-for-svn, but you can also just add 'Debug' to .gitignore if you don't use that. – Michael Chinen Jun 02 '15 at 17:47