Search code examples
gitgitignore

Git Ignore folders in specific paths


I have a rule on gitignore file to exclude folders with name "bar" but I want to "un-ignore" if the "bar" folder is below "foo"

  • ignore: bar/

  • but allow: foo/bar

Is it possible? How can I do it?

I tryed:

bar/
!foo/bar/

and

bar/
foo/*
!foo/bar/

and many other options but no luck.

what happens is that it ignores (prevent commit) both "bar" folders or none, I can't make it ignore only the one that is outside "foo"


Solution

  • Is it possible? How can I do it?

    The first solution you mentioned works.

    Output of git status --untracked-files=all . without the .gitignore file:

    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            bar/something.txt
            foo/bar/something.txt
    

    So we see two files are new and reported as "untracked".

    If we now create a .gitignore file with the following content

    bar/
    !foo/bar/
    

    the output changes to

    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            .gitignore
            foo/bar/something.txt
    

    so git

    • picks up our newly created .gitignore file
    • ignores the bar/something.txt which is still present on the filesystem
    • but does not ignore the foo/bar/something.txt