Bird
Raised Fist0
Gitdevops~10 mins

git add with patterns and directories - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - git add with patterns and directories
Start: Identify files in directory
Match files with pattern?
NoSkip file
Yes
Add matched file to staging area
More files?
YesRepeat matching
No
End: Files staged
Git scans files in directories, matches them against given patterns, and adds matched files to the staging area.
Execution Sample
Git
git add src/*.js
git add docs/
git add '*.md'
Add JavaScript files in src/, all files in docs/, and markdown files in current directory to staging.
Process Table
StepCommandFiles ScannedPattern MatchingFiles Added to Staging
1git add src/*.jssrc/app.js, src/util.js, src/readme.txtMatches: app.js, util.jssrc/app.js, src/util.js
2git add docs/docs/index.md, docs/guide.mdAll files in docs/ includeddocs/index.md, docs/guide.md
3git add '*.md'README.md, CHANGELOG.md, notes.txtMatches: README.md, CHANGELOG.mdREADME.md, CHANGELOG.md
4End--Staging contains all matched files from above commands
💡 All specified patterns and directories processed; matching files added to staging area.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Staging Areaemptysrc/app.js, src/util.jssrc/app.js, src/util.js, docs/index.md, docs/guide.mdsrc/app.js, src/util.js, docs/index.md, docs/guide.md, README.md, CHANGELOG.mdsrc/app.js, src/util.js, docs/index.md, docs/guide.md, README.md, CHANGELOG.md
Key Moments - 3 Insights
Why does git add '*.md' use quotes around the pattern?
Quotes prevent the shell from expanding the pattern before git runs, so git itself matches files. See step 3 in execution_table where '*.md' matches README.md and CHANGELOG.md.
Does git add docs/ add only files or also subdirectories?
It adds all files recursively inside docs/ including subdirectories. Step 2 shows all files in docs/ added to staging.
What happens if no files match the pattern?
No files are added for that command. The execution_table would show no files matched and staging remains unchanged for that step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which files were added to staging after step 1?
AREADME.md, CHANGELOG.md
Bdocs/index.md, docs/guide.md
Csrc/app.js, src/util.js
Dnotes.txt
💡 Hint
Check the 'Files Added to Staging' column for step 1 in execution_table.
At which step does git add include all files inside a directory?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the command adding a directory and the matching files in execution_table step 2.
If the pattern '*.md' was not quoted, what would happen?
AThe shell would expand the pattern before git runs.
BGit would match files itself as usual.
CNo files would be added to staging.
DGit would throw an error.
💡 Hint
Refer to key_moments explanation about quoting patterns and step 3 in execution_table.
Concept Snapshot
git add with patterns and directories:
- Use patterns like '*.js' to add matching files.
- Use directory names to add all files inside recursively.
- Quote patterns to prevent shell expansion.
- Staging area updates with matched files after each command.
Full Transcript
This visual execution shows how git add works with patterns and directories. First, git scans files in the specified directory. Then it matches files against the given pattern or includes all files if a directory is specified. Matched files are added to the staging area. For example, 'git add src/*.js' adds all JavaScript files in src/. 'git add docs/' adds all files inside docs/. Quoting patterns like '*.md' ensures git matches files itself, not the shell. The staging area accumulates all matched files after each command. This helps prepare exactly the files you want to commit.

Practice

(1/5)
1. What does the command git add src/ do?
easy
A. Removes the 'src' directory from the repository.
B. Stages only the 'src' directory itself, not its contents.
C. Commits changes in the 'src' directory immediately.
D. Stages all changes in the 'src' directory and its subdirectories.

Solution

  1. Step 1: Understand the command context

    The command git add src/ targets the directory named 'src'.
  2. Step 2: Know how git add works with directories

    Git stages all files and subdirectories inside 'src' recursively when adding a directory.
  3. Final Answer:

    Stages all changes in the 'src' directory and its subdirectories. -> Option D
  4. Quick Check:

    git add directory = stage all inside [OK]
Hint: Adding a directory stages all files inside it recursively [OK]
Common Mistakes:
  • Thinking it stages only the directory, not contents
  • Confusing add with commit
  • Assuming it deletes files
2. Which of the following is the correct command to stage all .txt files in the current directory using a pattern?
easy
A. git add txt.*
B. git add .txt*
C. git add *.txt
D. git add *txt

Solution

  1. Step 1: Understand wildcard usage in git add

    The asterisk (*) matches any characters, so *.txt matches all files ending with '.txt'.
  2. Step 2: Check each option's pattern

    git add *.txt correctly matches all '.txt' files. Others do not match the intended pattern.
  3. Final Answer:

    git add *.txt -> Option C
  4. Quick Check:

    Use * before extension to match all files [OK]
Hint: Use '*.ext' to add all files with that extension [OK]
Common Mistakes:
  • Placing * after extension instead of before
  • Using wrong order of characters in pattern
  • Confusing dot placement in patterns
3. Given the directory structure:
project/
  ├─ app.js
  ├─ README.md
  └─ docs/
      ├─ intro.md
      └─ setup.md

What files will be staged after running git add docs/*.md?
medium
A. Only docs/intro.md and docs/setup.md
B. All files in project including app.js and README.md
C. No files, command fails due to pattern
D. Only docs/setup.md

Solution

  1. Step 1: Analyze the pattern used

    The pattern docs/*.md matches all files ending with '.md' inside the 'docs' directory only.
  2. Step 2: Identify matching files

    Inside 'docs', 'intro.md' and 'setup.md' match. Files outside 'docs' are not matched.
  3. Final Answer:

    Only docs/intro.md and docs/setup.md -> Option A
  4. Quick Check:

    Pattern matches files only inside specified directory [OK]
Hint: Pattern 'dir/*.ext' adds matching files only inside that directory [OK]
Common Mistakes:
  • Assuming files outside 'docs' are staged
  • Thinking pattern matches recursively
  • Believing command fails if pattern matches multiple files
4. You run git add docs/*.md but only docs/intro.md is staged, not docs/setup.md. What is the most likely cause?
medium
A. You need to add files one by one manually.
B. The file docs/setup.md is ignored by .gitignore.
C. Git add cannot stage files in subdirectories.
D. The pattern '*.md' only matches one file at a time.

Solution

  1. 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.
  2. Step 2: Check other options for correctness

    Patterns can match multiple files; git add works recursively for directories; manual adding is not required.
  3. Final Answer:

    The file docs/setup.md is ignored by .gitignore. -> Option B
  4. Quick Check:

    Ignored files are skipped by git add [OK]
Hint: Check .gitignore if some matching files are not staged [OK]
Common Mistakes:
  • Believing patterns match only one file
  • Thinking git add can't stage multiple files
  • Assuming manual add is always needed
5. You want to stage all JavaScript files in the 'src' directory and all Markdown files in the 'docs' directory with one command. Which command achieves this?
hard
A. git add src/*.js docs/*.md
B. git add 'src/*.js docs/*.md'
C. git add src/*.js && git add docs/*.md
D. git add src/ docs/ *.js *.md

Solution

  1. Step 1: Understand how to add multiple patterns in one command

    Git add accepts multiple paths or patterns separated by spaces without quotes.
  2. 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.
  3. Final Answer:

    git add src/*.js docs/*.md -> Option A
  4. Quick Check:

    Multiple patterns separated by space work in one git add [OK]
Hint: List multiple patterns separated by spaces in one git add command [OK]
Common Mistakes:
  • Using quotes that prevent shell expansion
  • Trying to combine commands with && instead of one command
  • Mixing directories and patterns incorrectly