.gitignore: track one file in an ignored directory

I am trying to add one untracked file to a repo in a directory that is in .gitignore

.

I am using the following:

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

      

This usually works in the first child directory, but not in the format above. What am I missing?

+3


source to share


1 answer


One way would be to add one file. Once tracked, git will continue tracking the file regardless of whether it is ignored or not in any of yours .gitignore

.

So the simple usage below will work:

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

      


If you really want to fix your file .gitignore

, you will need to add another rule ( !parent/child/child-02

). This essentially says git

not to ignore the directory child-02

, so the following 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

      

+6


source







All Articles