What if you could add hundreds of files with just one simple command instead of typing each name?
Why git add with patterns and directories? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big project with hundreds of files and folders. You want to prepare only certain files for saving, like all images or all files in a specific folder. Doing this by picking each file one by one feels like sorting a huge pile of papers by hand.
Manually selecting files is slow and easy to mess up. You might forget some files or add the wrong ones. This wastes time and can cause mistakes in your project history.
Using git add with patterns and directories lets you quickly select groups of files by matching names or folder paths. It's like using a smart filter to grab exactly what you need in one go.
git add file1.txt git add file2.txt git add images/photo1.png
git add '*.txt'
git add images/This lets you work faster and safer by adding many files at once without missing or mixing them up.
When updating a website, you can add all changed CSS files with one command instead of typing each file name, saving time and avoiding errors.
Manually adding files is slow and error-prone.
Patterns and directories let you add many files quickly.
This makes managing project changes easier and safer.
Practice
git add src/ do?Solution
Step 1: Understand the command context
The commandgit add src/targets the directory named 'src'.Step 2: Know how git add works with directories
Git stages all files and subdirectories inside 'src' recursively when adding a directory.Final Answer:
Stages all changes in the 'src' directory and its subdirectories. -> Option DQuick Check:
git add directory = stage all inside [OK]
- Thinking it stages only the directory, not contents
- Confusing add with commit
- Assuming it deletes files
.txt files in the current directory using a pattern?Solution
Step 1: Understand wildcard usage in git add
The asterisk (*) matches any characters, so*.txtmatches all files ending with '.txt'.Step 2: Check each option's pattern
git add *.txt correctly matches all '.txt' files. Others do not match the intended pattern.Final Answer:
git add *.txt -> Option CQuick Check:
Use * before extension to match all files [OK]
- Placing * after extension instead of before
- Using wrong order of characters in pattern
- Confusing dot placement in patterns
project/
├─ app.js
├─ README.md
└─ docs/
├─ intro.md
└─ setup.mdWhat files will be staged after running
git add docs/*.md?Solution
Step 1: Analyze the pattern used
The patterndocs/*.mdmatches all files ending with '.md' inside the 'docs' directory only.Step 2: Identify matching files
Inside 'docs', 'intro.md' and 'setup.md' match. Files outside 'docs' are not matched.Final Answer:
Only docs/intro.md and docs/setup.md -> Option AQuick Check:
Pattern matches files only inside specified directory [OK]
- Assuming files outside 'docs' are staged
- Thinking pattern matches recursively
- Believing command fails if pattern matches multiple files
git add docs/*.md but only docs/intro.md is staged, not docs/setup.md. What is the most likely cause?Solution
Step 1: Understand why some files are not staged
If a file is ignored by .gitignore, git add will skip it even if the pattern matches.Step 2: Check other options for correctness
Patterns can match multiple files; git add works recursively for directories; manual adding is not required.Final Answer:
The file docs/setup.md is ignored by .gitignore. -> Option BQuick Check:
Ignored files are skipped by git add [OK]
- Believing patterns match only one file
- Thinking git add can't stage multiple files
- Assuming manual add is always needed
Solution
Step 1: Understand how to add multiple patterns in one command
Git add accepts multiple paths or patterns separated by spaces without quotes.Step 2: Evaluate each option
git add src/*.js docs/*.md correctly lists both patterns separated by space. git add 'src/*.js docs/*.md' quotes patterns unnecessarily, which may cause shell to treat them literally. git add src/*.js && git add docs/*.md runs two commands, not one. git add src/ docs/ *.js *.md mixes directories and patterns incorrectly.Final Answer:
git add src/*.js docs/*.md -> Option AQuick Check:
Multiple patterns separated by space work in one git add [OK]
- Using quotes that prevent shell expansion
- Trying to combine commands with && instead of one command
- Mixing directories and patterns incorrectly
