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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Ignore a single file name (matches at any depth under this .gitignore)
secrets.txt

# Ignore all .log files (matches at any depth under this .gitignore)
*.log

git filter-repo --force \
--path-glob '*.tar' \
--path-glob '*.tar.*' \
--path-glob '*.pth' \
--path-glob '*.png' \
--path-glob '*.mp4' \
--path-glob '*.pkl' \
--path-glob '*.pt' \
--invert-paths

Exclude Directories:

1
2
3
4
5
6
7
8
9
# Ignore any directory named node_modules at any depth
node_modules/

# Ignore any directory named temp at any depth
temp/

# If you only want to ignore the directory in the SAME folder as this .gitignore:
# /node_modules/
# /temp/

Exclude All Files In a SpecifiDirectory:

1
2
3
4
5
# Ignore all files directly under any directory named bin (at any depth)
bin/*

# Explained before
bin/

Include Specific Files or Directories:

The ! operator can be used to negate the ignore pattern and include specific files or directories.

1
2
3
4
5
6
gitignore
# Ignore everything in the logs directory
logs/*

# But include the .keep file
!logs/.keep

Using Wildcards and Patterns:

1
2
3
4
5
6
7
8
9
10
11
12
gitignore
# Ignore all .txt files in the doc/ directory
doc/*.txt

# Ignore all .pdf files in the root directory (but not in subdirectories)
/*.pdf

# Ignore all .tar.gz files in any directory
*.tar.gz

# Ignore files with a number as the filename
*[0-9]*

Comments:

1
2
gitignore
# This is a comment - it will be ignored by Git

Exclude Folders with Specific Names Anywhere in the Repository:

1
2
3
gitignore
# Ignore any folder named build wherever it is in the project
**/build/

Exclude Files with Specific Names Anywhere in the Repository:

1
2
3
gitignore
# Ignore any file named TODO wherever it is in the project
**/TODO

Practical Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
gitignore
# Ignore Mac system files
.DS_Store

# Ignore node_modules in any JavaScript project
node_modules/

# Ignore the build output directory
/dist

# Ignore log files
*.log

# Ignore all .env files
.env*

# Include .env.example file
!.env.example

# Ignore compiled Python files
*.pyc

# Ignore the database file in a Django project
*.sqlite3

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 logdir directory 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.

dir/

  • Pattern: logdir/
  • Meaning: This pattern ignores any directory named logdir and 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.