git except a sub directory and it's files of a ignored directory
Asked Answered
N

3

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
Nougat answered 2/6, 2015 at 5:5 Comment(1)
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 belowSnakemouth
S
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.

Stimulant answered 2/6, 2015 at 7:3 Comment(0)
S
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
Snakemouth answered 2/6, 2015 at 20:58 Comment(0)
S
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.

Sacking answered 2/6, 2015 at 5:26 Comment(1)
It's part of the git-extras suite #1677621, but you can also just add 'Debug' to .gitignore if you don't use that.Sacking

© 2022 - 2024 — McMap. All rights reserved.