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.

  • How do I manage git submodule subprojects in Eclipse?
  • Download git submodules for tarball
  • How to git fetch efficiently from a shallow clone
  • Pre-commit script creates mysqldump file, but “nothing to commit (working directory clean)”?
  • Working directory diff with Git Extensions
  • How do you make `git grep` output look like `ack` output?
  • 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.

  • git excluded files still showing up as untracked
  • Pass an argument to a Git alias command
  • virtualenv doesn't work on git bash MINGW32
  • Is Git recommended for large (>250GB) content repositories
  • Git push branch from one remote to another?
  • Git color code for contributor page
  • 4 Solutions collect form web for “git ignore exception not working as desired”

    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].

    As an ugly-but-working workaround, do this on every directory level on the path to your file:

    1. exclude all content of the directory
    2. re-include the sub-directory leading to your file (or finally, your file).

    Your entries would look like this, with each two-line section executing these steps for one level of the directory hierarchy:

    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
    

    So, you don’t start by excluding Demos/ like the question author did. It’s a parent directory of our file, so you would have to re-include it right afterwards. Instead, start by excluding its contents: Demos/**.

    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.

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

    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
    
    Git Baby is a git and github fan, let's start git clone.