GIT
  • What is a Version Control System ?
    • How Software would have been developed before '70s?
    • Expectations of a tool
    • Generation of Version Control System
    • The BitKeeper-Git-Mercurial Saga : How a License Agreement Change Shaped the Open-Source World
    • Mercurial was invented by andrew, then why Linus needs to create GIT
  • Git Installation and Initial Configs
  • Basic Terminologies
  • Q1 - Initialize a Repository and Basic Configurations
  • Q2 - Adding and Staging Files
  • Ways to Exclude Files in GIT ?
Powered by GitBook
On this page
  • 1. Exclude Files with git add Using Patterns
  • 2. Exclude Files Temporarily Using .gitignore
  • 3. Exclude Files Using git rm with Patterns
  • 4. Use git update-index to Skip Files
  • Use Glob Patterns in Command-Line
  • 5. Use Negation in .gitignore
  • 6. Exclude Files in Aliases

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.

  • .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.

PreviousQ2 - Adding and Staging Files

Last updated 6 months ago