gitignore
Sources:
- Github: Ignoring files
gitignore
You can put a .gitignore file under your working directory (the same level as the .git/ directory) to tell Git which paths to treat as “intentionally untracked.
Note:
.gitignore only affects untracked files.
If a file is already tracked (already committed), adding it to .gitignore will not stop tracking it—you must remove it from the repo (while keeping it locally), e.g.:
1
git rm --cached path/to/file
Then commit that change.
Exclude Files:
1 | # Ignore a single file name (matches at any depth under this .gitignore) |
Exclude Directories:
1 | # Ignore any directory named node_modules at any depth |
Exclude All Files In a SpecifiDirectory:
1 | # Ignore all files directly under any directory named bin (at any depth) |
Include Specific Files or Directories:
The ! operator can be used to negate the ignore pattern and include specific files or directories.
1 | gitignore |
Using Wildcards and Patterns:
1 | gitignore |
Comments:
1 | gitignore |
Exclude Folders with Specific Names Anywhere in the Repository:
1 | gitignore |
Exclude Files with Specific Names Anywhere in the Repository:
1 | gitignore |
Practical Examples:
1 | gitignore |
Remember, the patterns in a .gitignore file are relative to the location of the .gitignore file itself. Typically, you'd place this file in the root directory of your repository so it applies to the entire repo. After editing the .gitignore file, you need to commit it to your repository for the changes to take effect.
/dir/
Pattern:
/logdir/Meaning: This pattern ignores only the
logdirdirectory located at the root of the repository.Example
:
- It will match and ignore
logdir/in the root of the repository. - It will not match and ignore
subdir/logdir/,anotherdir/logdir/, etc.
- It will match and ignore
dir/
- Pattern:
logdir/ - Meaning: This pattern ignores any directory named
logdirand its contents, regardless of its location in the repository. - Example:
- It will match and ignore
logdir/in the root of the repository. - It will also match and ignore
subdir/logdir/,anotherdir/logdir/, and so on.
- It will match and ignore