3

I am trying to add a single non-tracked file to a repo within a directory that is in a .gitignore.

I am using the following:

parent/child/*
parent/child/child-02/*
!parent/child/child-02/file.txt

This usually works on a first-child directory but not in the format above. What Am I missing?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186

1 Answers1

8

One way would be to force add the single file. Once tracked, git will continue tracking the file irrespective of whether it is ignored or not in any of your .gitignore.

So simply using the below will work:

git add -f parent/child/child-02/file.txt 
git commit -m "added file.txt"

If you really want to correct your .gitignore file, you will have to add one more rule (!parent/child/child-02). This essentially tells git to not ignore the child-02 directory, and so the subsequent rules work:

parent/child/*
!parent/child/child-02
parent/child/child-02/*
!parent/child/child-02/file.txt

DEMO

test $ mkdir repo && cd repo && git init
repo $ mkdir -p parent/child/child-02
repo $ touch file parent/file parent/child/file parent/child/child-02/file.txt
repo $ cat .gitignore 
parent/child/*
!parent/child/child-02
parent/child/child-02/*
!parent/child/child-02/file.txt
repo $ git add .
repo $ git status
        new file:   .gitignore
        new file:   file
        new file:   parent/child/child-02/file.txt
        new file:   parent/file
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • `parent/child/* !parent/child/child-02 parent/child/child-02/* !parent/child/child-02/file.txt` Is what I was needing - Thnx! –  Apr 28 '15 at 14:28
  • I have to mention, that in `git status` you'll see whole directory `parent` marked as untracked. This confused me, so I used `git ls-files --others --exclude-standard` from [this answer](http://stackoverflow.com/a/3801554/1713660) to be sure I add only one file – vladkras Dec 13 '15 at 12:52