# gitignore excludes not working?

3 messages from 2013-12-06 to 2013-12-07. Participants: Martin Langhoff, Jiang Xin, Duy Nguyen.
Thread: https://gitlist.dev/t/35490

## Martin Langhoff, 2013-12-06 17:26

Subject: gitignore excludes not working?
Message-ID: <CACPiFCJ0Jj1M8KCrwus=DeRSBE2O3HKhvEo-UJhHoZDhHTzsSA@mail.gmail.com>
URL: https://gitlist.dev/e/CACPiFCJ0Jj1M8KCrwus%3DDeRSBE2O3HKhvEo-UJhHoZDhHTzsSA%40mail.gmail.com

```
Tested with git 1.7.12.4 (Apple Git-37) and git 1.8.3.1 on F20.

$ mkdir foo
$ cd foo
$ git init
Initialized empty Git repository in /tmp/foo/.git/
$ mkdir -p modules/boring
$ mkdir -p modules/interesting
$ touch modules/boring/lib.c
$ touch modules/interesting/other.c
$ touch modules/interesting/lib.c
$ git add modules/interesting/lib.c
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# new file:   modules/interesting/lib.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# modules/boring/
# modules/interesting/other.c

$ echo '/modules/' > .gitignore
$ echo '!/modules/interesting/' >> .gitignore

On this git status, I would expect to see modules/interesting/other.c
listed as untracked, however:

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# new file:   modules/interesting/lib.c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# .gitignore


thoughts?



m
-- 
 martin.langhoff@gmail.com
 -  ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 ~ http://docs.moodle.org/en/User:Martin_Langhoff

```

## Jiang Xin, 2013-12-07 01:07

Subject: Re: gitignore excludes not working?
Message-ID: <CANYiYbG=qdoLmD9FkOPikjVeN9T69+U6KBGYyVc5NAvuiZ1rLA@mail.gmail.com>
URL: https://gitlist.dev/e/CANYiYbG%3DqdoLmD9FkOPikjVeN9T69%2BU6KBGYyVc5NAvuiZ1rLA%40mail.gmail.com
In-Reply-To: <CACPiFCJ0Jj1M8KCrwus=DeRSBE2O3HKhvEo-UJhHoZDhHTzsSA@mail.gmail.com>

```
2013/12/7 Martin Langhoff <martin.langhoff@gmail.com>:
> Tested with git 1.7.12.4 (Apple Git-37) and git 1.8.3.1 on F20.
>
> $ mkdir foo
> $ cd foo
> $ git init
> Initialized empty Git repository in /tmp/foo/.git/
> $ mkdir -p modules/boring
> $ mkdir -p modules/interesting
> $ touch modules/boring/lib.c
> $ touch modules/interesting/other.c
> $ touch modules/interesting/lib.c
> $ git add modules/interesting/lib.c
> $ git status
> # On branch master
> #
> # Initial commit
> #
> # Changes to be committed:
> #   (use "git rm --cached <file>..." to unstage)
> #
> # new file:   modules/interesting/lib.c
> #
> # Untracked files:
> #   (use "git add <file>..." to include in what will be committed)
> #
> # modules/boring/
> # modules/interesting/other.c
>
> $ echo '/modules/' > .gitignore
> $ echo '!/modules/interesting/' >> .gitignore
>

I happened to write a software "Gistore" to backup arbitrary files and
directories using Git, and it will save interesting things in .git/info/exclude.
For your case, you could write .gitignore as:

    /modules/*
    !/modules/interesting
    !/modules/interesting/**

You can find my code in Gistore to generate proper .gitignore here:

    https://github.com/jiangxin/gistore/blob/master/lib/gistore/repo.rb#L228

Because I will ignore all files except what I want, the ignore file would be:

   *
    !/modules
    !/modules/interesting
    !/modules/interesting/**


-- 
Jiang Xin

```

## Duy Nguyen, 2013-12-07 03:20

Subject: Re: gitignore excludes not working?
Message-ID: <CACsJy8CyvSP9y7ui=2h7oFyeYCJK7WTdpqoSk00SM=LdpEokyQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8CyvSP9y7ui%3D2h7oFyeYCJK7WTdpqoSk00SM%3DLdpEokyQ%40mail.gmail.com
In-Reply-To: <CACPiFCJ0Jj1M8KCrwus=DeRSBE2O3HKhvEo-UJhHoZDhHTzsSA@mail.gmail.com>

```
On Sat, Dec 7, 2013 at 12:26 AM, Martin Langhoff
<martin.langhoff@gmail.com> wrote:
> # Untracked files:
> #   (use "git add <file>..." to include in what will be committed)
> #
> # modules/boring/
> # modules/interesting/other.c
>
> $ echo '/modules/' > .gitignore
> $ echo '!/modules/interesting/' >> .gitignore

Once you ignore the directory "modules", it won't bother with anything
inside. As a result, !/modules/interesting/ is ignored. Turning the
first rule to /modules/* like Jiang Xin showed does the trick because
git is told to ignore entries inside 'modules', not 'modules' itself.

I think the best we could do is turning /modules/ into /modules/*
internally. But it only works for simple cases like yours. If you
write /mo*ules/, I don't think we can make git smart enough to
correlate /mo*ules/ to !/modules/interesting/. So the second best
thing is improve documents about this case, which is what
kb/doc-exclude-directory-semantics does (currently on 'next').
-- 
Duy

```
