3

I have this file hierarchy in my project:

assets
  --script.js
  --a.html
  --admin
    --carousel.js
    --documentation.html

I want to add rules in .gitignore so that all files inside assets gets ignored except js files. I tried following in .gitignore that only considers script.js from first level:

assets
!assets/*.js

and then tried this which too only considers script.js :

assets
!assets/**/*.js

Other patterns like !assets//.js or !assets/**.js does not works. I want to consider all javascript files in all levels to consider as untracked.

I am trying this with Git version 2.1.0 .

jww
  • 97,681
  • 90
  • 411
  • 885
neotohin
  • 153
  • 1
  • 8
  • See http://stackoverflow.com/questions/25715898/whats-the-difference-between-git-ignoring-directory-and-directory/25716803#25716803 – jub0bs Sep 20 '14 at 07:23
  • possible duplicate of [Difference between \*\* and \* in glob matching (.gitignore)](http://stackoverflow.com/questions/8832729/difference-between-and-in-glob-matching-gitignore) – jww Sep 20 '14 at 07:27
  • @jubobs -- this does not provides the solution. I tried putting another .gitignore file inside assets folder but that did not worked. – neotohin Sep 20 '14 at 07:54
  • @jww that is a very old post from 2012 but seems he faced the similar issue. In that post there is no valid answer. – neotohin Sep 20 '14 at 19:04

2 Answers2

4

You should not ignore any directories or it will not look into these directories to include .js files under them again. Refer to What's the difference between Git ignoring directory and directory/*?.

So the .gitignore should be:

assets/**
!assets/**/
!assets/**/*.js
Community
  • 1
  • 1
Landys
  • 7,169
  • 3
  • 25
  • 34
0

may be files are already tracked. please untrack other files and add it to your ig

# Ignore everything
*
# Don't ignore directories, so we can recurse into them
!*/
# Don't ignore .gitignore and *.js files
!.gitignore
!*.js
g-newa
  • 459
  • 3
  • 10