阿里云主机折上折
  • 微信号
Current Site:Index > Ignore file mode

Ignore file mode

Author:Chuan Chen 阅读数:19585人阅读 分类: 开发工具

What is the Ignore File Pattern

The ignore file pattern is a mechanism in Git used to specify which files or directories should not be version-controlled. By creating a special file named .gitignore, developers can explicitly tell Git which content should be ignored. This feature is particularly useful for managing temporary files, build artifacts, local configuration files, and other similar items in a project.

The .gitignore file is typically placed in the root directory of the repository but can also appear in subdirectories. Git searches for .gitignore files starting from the current directory upward to the repository's root directory, then merges and applies all found rules.

Basic Syntax of Ignore Files

The .gitignore file uses simple pattern-matching syntax:

  1. Empty lines or lines starting with # are ignored (treated as comments).
  2. Standard glob pattern matching is used.
  3. A leading slash / prevents recursion.
  4. A trailing slash / specifies a directory.
  5. A leading exclamation mark ! indicates a negation pattern.
# Ignore all .log files
*.log

# But do not ignore important.log
!important.log

# Ignore the build folder in the root directory
/build/

# Ignore all temp folders in any directory
temp/

Common Ignore Pattern Examples

Ignoring Specific File Extensions

# Ignore log files
*.log

# Ignore build artifacts
*.o
*.a
*.so

Ignoring Specific Directories

# Ignore the node_modules directory
node_modules/

# Ignore all build directories
build/

# Ignore the .idea directory (JetBrains IDE configuration)
.idea/

Ignoring Files in Specific Paths

# Ignore files in a specific path
doc/*.txt

# But do not ignore txt files in doc subdirectories
!doc/**/*.txt

Advanced Ignore Patterns

Using Double Asterisks to Match Multi-level Directories

# Match logs directories at any level
**/logs/

# Match debug.log files at any level
**/debug.log

Excluding Specific Cases

# Ignore all .txt files
*.txt

# But do not ignore important.txt
!important.txt

# Ignore all files in the build directory
build/*

# But do not ignore the config file in the build directory
!build/config

Language-Specific Ignore Pattern Examples

JavaScript Projects

# Dependency directories
node_modules/

# Build artifacts
dist/
build/
*.min.js

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Environment variable files
.env
.env.local

Python Projects

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/

Global Ignore Files

In addition to project-specific .gitignore files, Git also supports configuring a global ignore file:

git config --global core.excludesfile ~/.gitignore_global

The global ignore file applies to all local repositories and is typically used to ignore editor temporary files, operating system-generated files, etc.:

# macOS
.DS_Store

# Windows
Thumbs.db

# Vim
*.swp
*.swo

Ignoring Already Tracked Files

Sometimes, you may need to ignore files that are already tracked by Git. In such cases, you must first stop tracking them:

git rm --cached <file>

Then add them to .gitignore. For example, to ignore an already tracked config.ini:

git rm --cached config.ini
echo "config.ini" >> .gitignore
git add .gitignore
git commit -m "Stop tracking config.ini"

Debugging Ignore Rules

If ignore rules are not working as expected, you can use the git check-ignore command to debug:

git check-ignore -v <file>

This will show which ignore rule matches the specified file. For example:

git check-ignore -v node_modules/package.json

Priority of Ignore Files

Git applies ignore rules in the following order:

  1. Ignore rules specified via the command line.
  2. The .gitignore file in the current directory.
  3. The .gitignore file in parent directories (recursively up to the repository root).
  4. $GIT_DIR/info/exclude.
  5. The file specified by the global core.excludesFile.

Rules defined later override earlier ones, and negation patterns (!) can override preceding ignore rules.

Best Practices in Real Projects

  1. Keep the .gitignore file tidy: Group rules by category and add explanatory comments.
  2. Consider committing template files: For example, config.sample.ini, allowing users to copy and configure it themselves.
  3. Do not ignore essential build tools: Such as Makefile or package.json.
  4. Regularly review ignore rules: As the project evolves, you may need to update the ignore rules.
# ================
# Project Build Artifacts
# ================
/dist/
/build/
/out/

# ================
# Dependency Directories
# ================
/node_modules/
/bower_components/

# ================
# Development Environment Files
# ================
.env
.env.local

Handling Special Cases

Ignoring Empty Directories

Git does not track empty directories by default, but sometimes you need to preserve the directory structure. A common solution is to add a .gitkeep file:

mkdir -p logs
touch logs/.gitkeep

Then, in .gitignore:

logs/*
!logs/.gitkeep

Ignoring Permission Changes

When developing across platforms, file permission changes can cause unnecessary diffs. You can configure Git to ignore file mode changes:

git config core.fileMode false

Cross-Platform Considerations

Different operating systems handle filename case sensitivity and path separators differently:

# Windows/Mac are case-insensitive; Linux is case-sensitive
# Explicitly specifying case can avoid issues
/Temp/
/temp/

It's best to use forward slashes / as path separators, as Git will automatically convert them to the current system's format.

Automatically Generating Ignore Files

Many tools can automatically generate .gitignore files tailored to specific project types:

  • gitignore.io
  • IDEs and frameworks often provide default ignore file templates.

For example, generating a .gitignore for a React project using gitignore.io:

curl https://www.toptal.com/developers/gitignore/api/node,react > .gitignore

Ignore Files and Git Attributes

.gitignore can be used in conjunction with .gitattributes files for more granular control:

# .gitattributes
*.min.js -diff -merge

This tells Git not to diff or merge minified JS files, even if they are tracked.

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.