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
git add -- ':!README.md'
Stage all .js
files except test.js
.js
files except test.js
git add -- '*.js' ':!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:
# Ignore README.md
README.md
# Ignore all .log files
*.log
# Ignore specific folder
/temp/
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
git add .
Remove README.md
from the staging area:
README.md
from the staging area:git restore --staged README.md
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
: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
:
.css
files except style.css
:git add *.css ':!style.css'
Exclude files with a certain prefix:
git add -- ':!temp_*'
5. Use Negation in .gitignore
.gitignore
You can exclude files and then re-include specific ones in .gitignore
.
Example .gitignore
:
.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..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