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¶
git add .
Remove README.md from the staging area:¶
git restore --staged README.md
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:¶
git update-index --skip-worktree README.md
Revert the skipping:¶
git update-index --no-skip-worktree README.md
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:¶
git add *.css ':!style.css'
Exclude files with a certain prefix:¶
git add -- ':!temp_*'
5. Use Negation in .gitignore¶
You can exclude files and then re-include specific ones in .gitignore.
Example .gitignore:¶
# Ignore all .txt files
*.txt
# Re-include important.txt
!important.txt
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:¶
git config --global alias.addexclude '!git add . && git restore --staged README.md'
Use the alias:¶
git addexclude
Key Points:
Patterns can be applied with
:!syntax for specific exclusions..gitignoreis a more permanent and repository-wide exclusion method.git update-indexworks for temporary file exclusions without modifying.gitignore.Glob patterns allow flexible inclusion/exclusion in commands.