Ways to Exclude Files in GIT ?

1. Exclude Files with git add Using Patterns

You can use git add with the -- separator and patterns to exclude specific files or directories.

Stage all files except README.md

git add -- ':!README.md'

Stage all .js files except test.js

git add -- '*.js' ':!test.js'

2. Exclude Files Temporarily Using .gitignore

Add patterns to a .gitignore file to exclude files or directories temporarily. Files listed in .gitignore will not be staged by git add.

Example .gitignore File:

# Ignore README.md
README.md

# Ignore all .log files
*.log

# Ignore specific folder
/temp/

3. Exclude Files Using git rm with Patterns

You can remove specific files or patterns from the staging area after they've been added.

Stage all files

Remove README.md from the staging area:

4. Use git update-index to Skip Files

You can manually mark files to be ignored temporarily by Git using the git update-index command.

Skip changes in README.md:

Revert the skipping:

Use Glob Patterns in Command-Line

Git supports glob patterns to include or exclude files when staging, committing, or viewing diffs.

Stage all .css files except style.css:

Exclude files with a certain prefix:

5. Use Negation in .gitignore

You can exclude files and then re-include specific ones in .gitignore.

Example .gitignore:

6. Exclude Files in Aliases

If you frequently need to exclude certain patterns, you can define a Git alias in your global configuration.

Add an alias in your Git config:

Use the alias:

Key Points:

  • Patterns can be applied with :! syntax for specific exclusions.

  • .gitignore is a more permanent and repository-wide exclusion method.

  • git update-index works for temporary file exclusions without modifying .gitignore.

  • Glob patterns allow flexible inclusion/exclusion in commands.

Last updated