git ignore exception not working as desired
Asked Answered
C

5

25

WinXP + mysisGit1.7

In my .gitignore file, but still can't see Demos/path/to/file/file.cpp being tracked by git.

I have below entries:

Demos/
!Demos/path/to/file/file.cpp

The absolute path is: c:\Project\Demos\path\to\file\file.cpp

What could be wrong? Please help, thanks.


EDIT:

I found the way how mysisGit .gitignore work on WindowsXP can only ignore certain type of file, then exclude some files with same type. For example:

*.bak
!tracking.bak
!/path/to/file/tracking2.bak

It doesn't work ignore folder and exclude some files under that folder. Below won't work:

/folderUnderRepoRoot/
!/folderUnderRepoRoot/tracking.cpp

Nor

anyFolderNamedLikeThis/
!anyFolderNamedLikeThis/tracking.cpp
!/anyFolderNamedLikeThis/tracking.cpp

However, I do find that there's an exception. There's a work-around way to exclude files just right under the ignored folder (not to its subfolder). This works.

/folderUnderRepoRoot/*
/folderUnderRepoRoot/tracking.cpp

But this way is only limited when the file is not in any subfolder, so it's not so useful.

So I end up still commit most of source files, even I was only interested in a few files while tracking some others big project. Which means there're a bunch of files I won't touch but still need to commit them.

Here is another thread that had similar problem.

Cherubini answered 8/9, 2010 at 13:2 Comment(3)
Did you git add Demos/path/to/file/file.cpp to start tracking it?Belted
@Stan: did you find a solution ? I found the same problem on git and ubuntu so it's not platform relatedHurlee
has my answer been helpful?Monti
A
30

Your .gitignore exception does not work because "It is not possible to re-include a file if a parent directory of that file is excluded" (source). This is "a performance related quirk in Git" (source).

As an ugly-but-functional workaround, do this for every directory in the path to your file:

  1. Exclude the contents of the directory.
  2. Re-include the sub-directory leading to your file (resp. finally, re-include your file).

Your entries would look like this, with each two-line section corresponding to the two steps above for one directory level:

Demos/*
!Demos/path/

Demos/path/*
!Demos/path/to/

Demos/path/to/*
!Demos/path/to/file/

Demos/path/to/file/*
!Demos/path/to/file/file.cpp

Notes:

The first line is not Demos/, unlike what the question author tried. Demos is a parent directory of our file, so we would have to re-include it right afterwards – means, we do not have to exclude it in the first place. Instead, we start by excluding only its contents: Demos/*.

Armorer answered 24/8, 2014 at 18:53 Comment(2)
I like this answer, but I needed just a single-asterisk Demos/* , Demos/path/*, instead of two asterisks, **. From the documentation: Example to exclude everything except a specific directory foo/barHeterogony
@TheRedPea You're right! I have adapted my answer. A trailing /** is only different when re-including (!… lines). When excluding, it's just the same as /*.Armorer
C
3

Do add to .gitignore as follows:

folderUnderRepoRoot/**/*
!folderUnderRepoRoot/tracking.cpp

But make sure, that at least a one file added to git index from the folderUnderRepoRoot folder.

Coccidiosis answered 27/11, 2013 at 9:0 Comment(0)
J
2

If it's just one file, I wouldn't normally modify .gitignore

The following should ignore .gitignore and allow you to add the file

git add -f Demos/path/to/file/file.cpp
Jahncke answered 5/10, 2015 at 14:8 Comment(0)
R
0

Can you try replacing it with !/Demos/path/to/file/file.cpp (note the leading slash after the exclamation mark) instead and try again?

Responsive answered 8/9, 2010 at 13:4 Comment(0)
S
0

for me, the case was so much different. I tried to ignore all files starting with "secrets" except this file "secrets.[stage].json" as it is just a template for developers.
This doesn't work


secrets*

!secrets.[stage].json


I found out that it was just because bracket is a special character that need to be escaped so this was the solution


secrets*

!secrets.[stage].json


The same problem happens with other special characters.

As .gitignore uses globbing patterns to match against file names. You can find the documentation of globbing patterns here https://linux.die.net/man/7/glob Which was much better for me than the official .gitignore documentation.

Shall answered 3/2, 2022 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.