Where does the .gitignore file belong?
Asked Answered
P

10

481

Does the .gitignore file belong in the .git folder structure somewhere or in the main source files?

Ptolemy answered 18/4, 2011 at 3:14 Comment(1)
You can have a .gitignore in every single directory of your project. However, the best practice is to have on single .gitignore file on the project root directory, and place all files that you want to ignore in it.Pleione
F
519

Put .gitignore in the working directory. It doesn't work if you put it in the .git (repository) directory.

$ ls -1d .git*
.git
.gitignore
Fallen answered 30/9, 2013 at 15:58 Comment(4)
It's an ambiguous answer IMO. Where in the working directory? In the root of it? Anywhere?Musicale
@CarlesAlcolea , The answer says you can place the .gitignore file anywhere in the working directory, i.e in any folder where your code prevails. Having said that, the best practice would be to place the .gitignore file in the root directory. This means one .gitignore file for one entire repo. This makes managing the ignored files more effectively.Cumberland
I need some further explanation. What happens if .gitignore is placed in a subdirectory? Does it just regulate the subdirectory and its branches? Is it possible to have multiple .gitignore's in different subdirectories? Would having .gitignores with different rules in subdirectories cascade over .gitignores of parrent directory?Misspend
@BehroozKarjoo - As per the documentation, the gitignore applies to all files in the same directory (or any subdirectories) - type "git help ignore" for the documentation.Striction
S
86

As the other answers stated, you can place .gitignore within any directory in a Git repository. However, if you need to have a private version of .gitignore, you can add the rules to .git/info/exclude file.

Scirrhus answered 18/4, 2011 at 3:38 Comment(3)
Also, core.excludesfile (see git-config(1)) to specify a file that holds your personal exclude patterns (your favorite “temporary file” naming pattern, your editor’s backup/temporary files, etc.). Accordingly, avoid putting “personal patterns” in tracked .gitignore file(s).Mawkish
The private part of this answer was very helpful, since I have a few modifications to the project that don't belong into the checked-in .gitignore file.Xenomorphic
If you want to store "private" files locally, you can also add folders to your working copy that have their own .gitignore that just says * (i.e., everything in this folder), and put the files there. That's how I do it. The "personal patterns" then go in an untracked .gitignore - since the file also says to ignore itself, Git won't offer to add it to version control.Palaeo
B
60

You can place .gitignore in any directory in git.

It's commonly used as a placeholder file in folders, since folders aren't usually tracked by git.

Battiste answered 18/4, 2011 at 3:16 Comment(2)
why are you allowed to put it anywhere? How come git doesn't get confused?Heterolysis
So the .gitignore file can go in the .git directory itself, can it? (Which was the original question). What purpose would that serve?Magenta
N
50

When in doubt just place it in the root of your repository. See https://help.github.com/articles/ignoring-files/ for more information.

Noam answered 18/4, 2011 at 3:21 Comment(0)
H
33

In the simple case, a repository might have a single .gitignore file in its root directory, which applies recursively to the entire repository. However, it is also possible to have additional .gitignore files in subdirectories. The rules in these nested .gitignore files apply only to the files under the directory where they are located. The Linux kernel source repository has 206 .gitignore files.

-- this is what i read from progit.pdf(version 2), P32

Hoodmanblind answered 11/7, 2020 at 0:48 Comment(0)
N
13

If you want to do it globally, you can use the default path git will search for. Just place it inside a file named "ignore" in the path ~/.config/git

(so full path for your file is: ~/.config/git/ignore)

Niigata answered 4/6, 2014 at 10:13 Comment(0)
G
7

Root directory is fine for placing the .gitignore file.

Don't forget to use git rm --cached FILENAME to add files to .gitignore if you have created the gitignore file after you committed the repo with a file you want ignored. See github docs. I found this out when I created a .env file, then committed it, then tried it to ignore it by creating a .gitignore file.

Guyot answered 21/1, 2021 at 23:27 Comment(0)
T
4

From the official documentation:

Git checks gitignore patterns from multiple sources, with the following order of precedence (the last matching pattern decides the outcome):

  1. Patterns read from the command line for those commands that support them.

  2. Patterns read from a .gitignore file in the same directory as the path, or in any parent directory (up to the top-level of the working tree), with patterns in the higher level files being overridden by those in lower level files down to the directory containing the file.

  3. Patterns read from $GIT_DIR/info/exclude.

  4. Patterns read from the file specified by the configuration variable core.excludesFile.

Tooling answered 3/11, 2022 at 11:38 Comment(0)
I
3

You may also find a global .gitignore directly at the ~ path if you haven't created it in your folder project. This file is taken into account by all your .git projects.

Investiture answered 11/4, 2018 at 12:18 Comment(2)
This is not working for me in Windows Server 2016. However, p[utting the contents in ~/.config/git/ignore as noted in @liran's post above this one does work.Primal
@jessewolfe: thanks for your return on Windows because my answer only dealt with Linux. 😉Investiture
C
0

Also, if you create a new account on Github you will have the option to add .gitignore and it will be setup automatically on the right/standard location of your working place. You don't have to add anything in there at the begin, just alter the contents any time you want.

Changeling answered 24/10, 2016 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.