willworth.dev
View RSS feed

Git ignore guide

Published on

A Guide to Git Ignore: What It Is and How to Set Up a Global Git Ignore File

1. What is .gitignore?

Git is a powerful version control system that tracks changes to files in a repository. However, not every file in your project should be tracked—some files are specific to your local environment, build processes, or operating system. The .gitignore file allows you to specify which files and directories Git should ignore, preventing them from being accidentally committed to your repository.

Why Use a .gitignore File?

  • Keeps repositories clean by avoiding unnecessary files.
  • Prevents sensitive data (like environment secrets) from being exposed.
  • Reduces clutter in commit history.
  • Speeds up performance by ignoring large files that aren't needed in version control.

2. What Should Always Go in a .gitignore File?

Though the contents of a .gitignore file depend on the type of project, some general rules apply across the board. Below are some common categories of files that should be ignored:

Operating System Files

Different OS environments generate system-specific files that should not be tracked:

# macOS
.DS_Store

# Windows
Thumbs.db
desktop.ini

Dependency Files

Most projects have package managers that generate dependency folders which should not be committed:

# Node.js
node_modules/

# Python
__pycache__/
venv/

# Ruby
.bundle/

Build Artifacts

Compiled files and directories should not be included in version control:

/dist/
/build/
/out/
*.log

Environment Files

Environment-specific configuration files should be excluded to avoid leaking sensitive credentials:

.env
.env.local
.env.*

IDE and Editor Files

Different development environments generate their own configuration files, which should not be tracked:

.vscode/
.idea/
*.swp
*.swo
*.bak

3. How to Set Up a Global Git Ignore File

Instead of adding the same ignore rules to every repository, you can create a global .gitignore file, which applies to all Git repositories on your system.

Steps to Set Up a Global Git Ignore

  1. Create the global ignore file

    Run the following command to create and configure the global .gitignore file:

    git config --global core.excludesfile ~/.gitignore_global
  2. Open the global ignore file in your preferred editor:

    code ~/.gitignore_global # Opens in VS Code
    nano ~/.gitignore_global # Opens in Nano (CLI editor)
    open -e ~/.gitignore_global # Opens in TextEdit (macOS)
  3. Add the recommended ignore rules. See the next section for suggestions.

4. What Should Go in the Global Git Ignore File?

The global ignore file should contain rules that apply to your local environment and development tools, across all projects. Here are some useful entries:

OS-Specific Files

# macOS
.DS_Store
.AppleDouble

# Windows
Thumbs.db
desktop.ini
$RECYCLE.BIN/

Editor/IDE Files

# VS Code
.vscode/

# JetBrains
.idea/

# Emacs
*~

# Vim
*.swp
*.swo

Log and Cache Files

*.log
npm-debug.log
yarn-error.log

System and Backup Files

*.bak
*.tmp

Check if Global Ignore is Set Up Correctly

After setting it up, verify the configuration with:

git config --global --get core.excludesfile

If set up correctly, it should output the path to your global ignore file.

5. Additional Tips

  • Use templates: GitHub provides a collection of .gitignore templates for various programming languages and frameworks. You can find them at github.com/github/gitignore.

  • Check ignored files: Run git check-ignore -v <file> to verify if a file is being ignored and why.

  • Remove ignored files from tracking: If you've accidentally committed an ignored file, remove it from the repository with:

    git rm --cached <file>

Conclusion

Setting up a good .gitignore file is essential to maintaining a clean and efficient Git repository. Whether it's project-specific or global, carefully curated ignore rules will save you from tracking unnecessary files and prevent potential security risks. Take a few minutes to configure your .gitignore properly, and you'll enjoy a smoother development experience!