.gitignore for directories at a specific depth?

I have directories at components/root/js/ and components/example/js/, and I don’t want these to be tracked by Git.

However, I have a directory components/root/js2/plugins/js/, which I want to have tracked by Git.

  • Why does git:// works but git@ does not
  • When considering git vs svn for work projects, is Mercurial best of both worlds?
  • Specify files with *
  • Relink with a Github Repository on a New Machine
  • Not visible GIT push
  • Git pathspec error - archive
  • I’ve looked up on this online and only found **, which I believe does not solve my problem.

    My current .gitconfig contains the line:


    I’ve tried changing this to:


    But this matches for components/root/js/, components/example/js/, but also components/root/js2/plugins/js/ (a js folder below the second level of components – I only want to ignore js folders at the second level).

    How can I achieve this goal?

  • Trouble un-ignoring previously ignored files in Git repo
  • Delete and Redeploy Rails app to heroku
  • Git workflow for different versions of a framework
  • How to configure Elastic Beanstalk for RDS
  • Git Commit message structure
  • What is the workflow to manage git branch history appropriately when working on 3+ branches simultaneously?
  • 2 Solutions collect form web for “.gitignore for directories at a specific depth?”

    OK, how about this:


    It will ignore all directories called js except for ones specified after !. In such scenario git will ignore components/root/js/ but won’t ignore components/root/js2/plugins/js/.

    ** won’t work because as it’s said in man gitignore:

    A trailing “**” matches everything inside. For example, “abc/**”
    matches all files inside directory “abc”, relative to the location of
    the .gitignore file, with infinite depth.

    You can put .gitigore files in any directory which will be a more reliable solution if the directories are moved later.

    I would create 2 .gitignore files:

    • One at the project root /.gitignore which excludes js


    • One in the plugins directory /components/root/js2/plugins/.gitignore which overrides root .gitignore


    If the plugins directory will be moved later, e.g. to components/root/plugins the .gitignore will be moved too.

    If you configure everything in the root .gitignore you must not forget to edit the root .gitignore if the directory is moved.

    Git Baby is a git and github fan, let's start git clone.