Does the .gitignore file belong in the .git folder structure somewhere or in the main source files?
Put .gitignore in the working directory. It doesn't work if you put it in the .git (repository) directory.
$ ls -1d .git*
.git
.gitignore
.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 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.
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 .gitignore
file. –
Xenomorphic .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 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.
When in doubt just place it in the root of your repository. See https://help.github.com/articles/ignoring-files/ for more information.
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
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
)
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.
From the official documentation:
Git checks gitignore patterns from multiple sources, with the following order of precedence (the last matching pattern decides the outcome):
Patterns read from the command line for those commands that support them.
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.Patterns read from
$GIT_DIR/info/exclude
.Patterns read from the file specified by the configuration variable
core.excludesFile
.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.