Ways to Exclude Files in GIT ?
1. Exclude Files with git add
Using Patterns
git add
Using PatternsYou can use git add
with the --
separator and patterns to exclude specific files or directories.
Stage all files except README.md
README.md
Stage all .js
files except test.js
.js
files except test.js
2. Exclude Files Temporarily Using .gitignore
.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:
3. Exclude Files Using git rm
with Patterns
git rm
with PatternsYou 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:
README.md
from the staging area:4. Use git update-index
to Skip Files
git update-index
to Skip FilesYou can manually mark files to be ignored temporarily by Git using the git update-index
command.
Skip changes in README.md
:
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
:
.css
files except style.css
:Exclude files with a certain prefix:
5. Use Negation in .gitignore
.gitignore
You can exclude files and then re-include specific ones in .gitignore
.
Example .gitignore
:
.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