I understand that a .gitignore file cloaks specified files from Git\'s version control. I have a project (LaTeX) that generates lots of extra files (.auth, .dvi, .pdf, logs,
Had the similar issue as OP but none of top 10 upvoted answer actually worked.
I finally found out the following
Wrong syntax :
*
!bin/script.sh
Correct syntax :
*
!bin
!bin/script.sh
Explanation from gitignore man page :
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
Which means that "Wrong syntax" above is wrong because bin/script.sh
cannot be reincluded as bin/
is ignored. That's all.
Extended example :
$ tree .
.
├── .gitignore
└── bin
├── ignore.txt
└── sub
└── folder
└── path
├── other.sh
└── script.sh
$ cat .gitignore
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
$ git status --untracked-files --ignored
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
.gitignore
bin/sub/folder/path/script.sh
Ignored files:
(use "git add -f ..." to include in what will be committed)
bin/ignore.txt
bin/sub/folder/path/other.sh
nothing added to commit but untracked files present (use "git add" to track)