gitignore

Creating a .gitignore file is essential for managing which files and directories Git should ignore in your repository. Here are the basics:

1. Purpose of .gitignore

  • Prevent accidental commits: Avoid committing sensitive data, temporary files, or build artifacts.
  • Keep repository clean: Ensure that only necessary files are tracked by Git.

2. Syntax

  • Each line in a .gitignore file specifies a pattern for files or directories to ignore.
  • Comments: Lines starting with # are comments.
  • Blank lines: Blank lines are ignored.

3. Patterns

  • Wildcards: Use * to match zero or more characters.
    • Example: *.log ignores all .log files.
  • Directories: To ignore a directory, include a trailing slash /.
    • Example: build/ ignores the build directory.
  • Negation: Patterns starting with ! negate a previously ignored pattern.
    • Example: !important.log ensures important.log is tracked, even if *.log is ignored.
  • Relative paths: Patterns are relative to the location of the .gitignore file.

Cheatsheet

Certainly! Here’s a comprehensive .gitignore cheatsheet, covering patterns from common to advanced use cases:

.gitignore Cheatsheet

Basic Patterns

  1. Ignoring Specific Files

    # Ignore all .log files
    *.log
  2. Ignoring Directories

    # Ignore a directory and its contents
    mydir/
  3. Ignoring Files by Name

    # Ignore all files named 'temp'
    temp

Wildcards

  1. Single Character Wildcard

    # Ignore all files with a single character followed by 'file.txt'
    ?file.txt
  2. Zero or More Characters Wildcard

    # Ignore all .log files in any directory
    **/*.log

Negation

  1. Negate a Pattern

    # Ignore all .log files except important.log
    *.log
    !important.log
  2. Negate a Directory Pattern

    # Ignore all directories named 'mydir' but not 'mydir' at root level
    mydir/
    !/mydir/

Relative Paths

  1. Relative Path Patterns

    # Ignore file at root level
    /file.txt
  2. Ignore Subdirectory Files

    # Ignore all 'file.txt' in any subdirectory of 'mydir'
    mydir/**/file.txt

Advanced Patterns

  1. Composite Patterns

    # Ignore all .log files in any directory but not in 'logs' directory
    **/*.log
    !logs/**/*.log
  2. Ignoring Files in Subdirectories

    # Ignore all 'config.json' files except in 'src' directory
    config.json
    !src/config.json
  3. Combining Patterns

    # Ignore all 'temp' files, but not in 'build' and 'dist' directories
    temp
    !build/temp
    !dist/temp
  4. Complex Negation

    # Ignore all 'bin' files except in the 'tools' directory
    bin/
    !tools/bin

Common Patterns

  1. Operating System Files

    # macOS
    .DS_Store
     
    # Windows
    Thumbs.db
    desktop.ini
  2. IDE/Editor Files

    # VS Code
    .vscode/
     
    # IntelliJ IDEA
    .idea/
    *.iml
  3. Programming Languages

    • Python

      __pycache__/
      *.pyc
    • Node.js

      node_modules/
      npm-debug.log
    • Java

      *.class

Specific Use Cases

  1. Binary vs Directory

    # Ignore the binary file 'mydir' at the root
    /mydir
    # Do not ignore the directory named 'mydir'
    !/mydir/
  2. Ignoring Specific File Types in Specific Directories

    # Ignore all .txt files in 'docs' directory
    docs/*.txt
  3. Ignore Files with Specific Extensions Globally

    # Ignore all .bak files globally
    *.bak

Global .gitignore

  1. Global Ignoring for All Repositories

    # Create a global .gitignore file
    touch ~/.gitignore_global
     
    # Add patterns to the global .gitignore
    echo '.DS_Store' >> ~/.gitignore_global
     
    # Configure Git to use the global .gitignore
    git config --global core.excludesfile ~/.gitignore_global

Best Practices

  • Global .gitignore: For ignoring files globally across all repositories, create a global .gitignore file and configure Git to use it:

    git config --global core.excludesfile ~/.gitignore_global
  • Repository-specific .gitignore: Place the .gitignore file in the root of your repository.

  • Review regularly: Regularly update your .gitignore file to reflect changes in your project structure and environment.

Example .gitignore File

Here is an example of a typical .gitignore file for a Node.js project:

# Node.js
node_modules/
npm-debug.log
 
# Logs
logs/
*.log
yarn-debug.log*
yarn-error.log*
 
# OS files
.DS_Store
Thumbs.db
 
# IDE files
.vscode/
.idea/
*.iml

By following these basics, you can effectively manage which files and directories are tracked by Git in your repository.


If you have a binary and directory of same name

If you have a directory and a binary inside it with the same name, and you only want to ignore the binary, you can achieve this using a combination of the .gitignore patterns.

Example Scenario

Let’s say you have the following structure:

myproject/
├── mydir/
│   └── file.txt
└── mydir  (binary file)

.gitignore Configuration

To ignore only the binary file and not the directory, you can use the following patterns in your .gitignore file:

/mydir    # Ignore the binary file named 'mydir'
!/mydir/  # Do not ignore the directory named 'mydir'

Explanation

  • /mydir tells Git to ignore the mydir file at the root of the repository.
  • !/mydir/ tells Git not to ignore the mydir directory and its contents.

Full Example

Here’s a complete example of what your .gitignore file might look like:

# Ignore the binary file named 'mydir'
/mydir
 
# Do not ignore the directory named 'mydir'
!/mydir/

Verification

After setting up the .gitignore file, you can verify that the binary file is ignored and the directory is not by running the following commands:

  1. Check the ignored status:

    git status --ignored
  2. List all ignored files:

    git ls-files --ignored --exclude-standard