13

How to ignore parent directory in .gitignore? I tried this patterns, but seems they are don't work:

/../*
../
../*
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • 4
    I'm a bit curious why don't you create another `.gitignore` in the parent folder instead of specifying it in it's child's `.gitignore` file – Dzung Nguyen Oct 21 '13 at 07:55
  • Afaik it is not possible (see @nXqd s answer). Also I think it is _highly_ confusing, if you (and of course git itself) must search not only the parent folders, but also _every_ subfolder and sub-subfolder to find out, which folders are ignored and why. – KingCrunch Oct 21 '13 at 07:58
  • 1
    I'm confused. Why do you want to ignore a parent directory in `.gitignore`? Is the parent directory in the same git repository? – johnsyweb Oct 21 '13 at 08:47

2 Answers2

17

If you want to ignore a folder but don't want to modify an existing .gitignore, drop a .gitignore in the folder containing only an asterisk.

*

Here is a quick BASH example to accomplish this task for the .idea folder:

$ echo '*' > .idea/.gitignore
Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45
14

You can't, if you want your .gitignore to be versioned.

And if it is versioned, that means elements of the parent folder is versioned as well (and cannot be ignored without removing from the index all of its content).
Kingcrunch comments you could still version the .gitignore with a git add -f, but as commented before, this would be quite confusing.

If it best to place the .gitignore in the parent folder of the folder you want to ignore.
And there declare the name of the folder you want to ignore:

folder_to_ignore/

(Note the final '/')

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    You can still add ignored files with `git add -f filename`. Thats a common use case when you want to track empty folders. "ignore" just means, that it is hidden `git status` and `git add` (without `-f`) – KingCrunch Oct 21 '13 at 07:59
  • @KingCrunch true, I have added that in the answer. – VonC Oct 21 '13 at 08:02
  • 1
    You can have multiple `.gitignore` files in different directories. An easy fix is to create another `.gitignore` file in the parent directory, then add the file you wish to ignore to it. – Isaac Lyman Oct 11 '17 at 15:35
  • 1
    @IsaacLyman yes: you can see all the .gitignore files with: `git check-ignore -v -- afile` – VonC Oct 11 '17 at 15:39
  • So if I have a git repo with many sub-projects or sub-folders, and I only want to ignore files in one sub-project, I cannot? – IgorGanapolsky Sep 19 '18 at 10:48
  • 1
    @IgorGanapolsky Sure you can: see the second part of my other answer at https://stackoverflow.com/a/50886524/6309. – VonC Sep 19 '18 at 10:53