Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git ignore exception not working as desired

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.

like image 296
Stan Avatar asked Sep 08 '10 13:09

Stan


People also ask

Why is Git ignore not ignoring?

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them. To ignore them, you first need to delete them, git rm them, commit and then ignore them.

How do I force Git to ignore a file?

You can create a . gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the . gitignore file in to your repository.

What is exclamation mark in Gitignore?

An exclamation mark can be used to match any character except one from the specified set. debug[a-z].


3 Answers

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/*.

like image 74
tanius Avatar answered Oct 06 '22 10:10

tanius


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.

like image 39
Малъ Скрылевъ Avatar answered Oct 06 '22 09:10

Малъ Скрылевъ


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
like image 43
cortopy Avatar answered Oct 06 '22 10:10

cortopy