gitignore

Sources:

  • Github: Ignoring files

Examples

Exclude Specific Files:

1
2
3
4
5
6
gitignore
# Ignore a single file
secrets.txt

# Ignore all .log files
*.log

Exclude Specific Directories:

1
2
3
4
5
6
gitignore
# Ignore an entire directory
node_modules/

# Ignore all directories named 'temp'
temp/

Exclude All Files In a Directory:

1
2
3
4
5
6
gitignore
# Ignore all files in the bin directory
bin/*

# Ignore all files in all directories named build
build/*

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.