Tom Xue
Tom Xue

Reputation: 3355

How to ignore some special file in gitignore?

I want to ignore some special file in .gitignore. E.g. symTbl.c, there will be some this kind of files generated by compiler and they are in different sub-directories. Meanwhile I need to accept all the other .c files.

So my .gitignore will be like below:

*
!*/
!*.c
symTbl.c

But still the symTbl.c cannot be ignored. How to do then? Thanks!

Note: I have some other files which should not be tracked, e.g. .lzs .bin... So I need to ignore * first and then use !*.c to track *.c. By doing so, I cannot simply put symTbl.c in .gitignore.

Upvotes: 1

Views: 224

Answers (1)

Carlos Campderrós
Carlos Campderrós

Reputation: 22972

You need to put just

symTbl.c

in your .gitignore file in the root of your project. Files already tracked cannot be ignored. Check with git ls-files | grep symTbl.c if you have already commited one and delete it (git ls-files | grep symTbl.c | xargs -d'\n' rm to delete them directly)

EDIT

I've done a new repo and put these files in it

# show all files, excluding those in the .git directory
$ tree -a -I .git
.
├── bla.bin
├── bla.c
├── foo
│   ├── bla.bin
│   ├── bla.c
│   └── symTbl.c
├── .gitignore
└── symTbl.c

1 directory, 7 files

My .gitignore contains what you had:

$ cat .gitignore
*
!*/
!*.c
symTbl.c

And running git add . just added the different bla.c files in the root and in the foo/ directory:

$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   bla.c
#   new file:   foo/bla.c
#

So probably you have already committed your symTbl.c files? If so, git rm them and they will be properly ignored from now on.

Upvotes: 2

Related Questions