Q2 - Adding and Staging Files
Problem Statement
Add Specific Files Create two files,
app.js
andstyle.css
. Usegit add
to stage onlystyle.css
. This allows selective addition of files to the staging area before committing.Interactive Staging Modify
style.css
and usegit add -p
to stage changes interactively. This lets you review and stage specific parts of a file.Stage All Files Except One Add all files in the folder to the staging area except
README.md
. Use a wildcard pattern to exclude the file, ensuring other files are prepared for commit.
Solution
1. Add Specific Files
Create the files
app.js
andstyle.css
touch app.js style.css
Stage only
style.css
git add style.css
Verify the staged files
git status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: style.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
app.js
2. Interactive Staging
Modify
style.css
(e.g., add some CSS code)
/* Example content for style.css */
body {
background-color: #f0f0f0;
}
Use interactive staging to review changes
git add -p style.css
Git will present changes in chunks and prompt for action
Stage this hunk [y,n,q,a,d,s,e,?]?
Actions:
Press
y
to stage the current chunk.Press
n
to skip the current chunk.Press
q
to quit without staging more changes.
Verify the staged changes
git status
Example Output,
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: style.css
3. Stage All Files Except One
Create a new file
README.md
alongsideapp.js
andstyle.css
:
touch README.md
Use a wildcard pattern to stage all files except
README.md
:
git add -- ':!README.md'
Verify the staged files
git status
Verify the staged files
git status
Verify the status
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: app.js
new file: style.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
Last updated