Q2 - Adding and Staging Files#
Problem Statement#
Add Specific Files
Create two files,app.jsandstyle.css. Usegit addto stage onlystyle.css. This allows selective addition of files to the staging area before committing.Interactive Staging
Modifystyle.cssand usegit add -pto 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 exceptREADME.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.jsandstyle.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
yto stage the current chunk.Press
nto skip the current chunk.Press
qto 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.mdalongsideapp.jsandstyle.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