Andy Song
Andy Song

Reputation: 167

Strange behavior of .gitignore

Here you go:

root@Dell: /tmp # mkdir test; cd test
root@Dell: /tmp/test # git init
Initialized empty Git repository in /tmp/test/.git/
root@Dell: /tmp/test # mkdir foo
root@Dell: /tmp/test # touch foo/a
root@Dell: /tmp/test # git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo/
nothing added to commit but untracked files present (use "git add" to track)
root@Dell: /tmp/test # cat > .gitignore << EOF
> *
> !foo/
> EOF
root@Dell: /tmp/test # git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

Can anybody explain why an empty .gitignore is not equivalent to the one with:

*
!foo/

in this case?

Upvotes: 2

Views: 383

Answers (2)

CB Bailey
CB Bailey

Reputation: 791411

The pattern * will match anywhere in your working tree, including matching any files in directories called foo.

Looking at your tree, what you probably want to do is only match all entites at the top level of your tree, except for directories called foo, so that anything under foo will still be tracked by default.

/*
!foo/

It may also make sense to root the second patter at the root of the working tree too.

/*
!/foo/

Upvotes: 1

VonC
VonC

Reputation: 1323183

You managed to make the directory foo "not ignored", but everything within foo is still matched by the '*' pattern.
This will remove foo content from the gitignore '*' initial directive:

*
!/foo/
!/foo/*

or:

*
!/foo/

plus a .gitignore in foo with:

!*

Upvotes: 4

Related Questions