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
Why staging before committing matters
📖 Scenario: You are working on a project with multiple files. You want to save your changes carefully so that only the right files are included in your commit history. This helps keep your project clean and organized.
🎯 Goal: Learn how to use git add to stage specific files before committing them with git commit. Understand why staging is important to control what goes into your commit.
📋 What You'll Learn
Create two new files with specific content
Stage only one file using git add
Commit the staged file with a message
Check the commit history to confirm only the staged file was committed
💡 Why This Matters
🌍 Real World
In real projects, you often change many files but want to commit only some of them. Staging lets you control exactly what goes into each commit, keeping your history clean and meaningful.
💼 Career
Understanding staging and committing is essential for collaborating in teams, managing code versions, and avoiding mistakes that can cause bugs or confusion.
Progress0 / 4 steps
1
Create two files with content
Create two files named file1.txt and file2.txt with the exact content: Hello from file1 and Hello from file2 respectively.
Git
Hint
Use echo with redirection > to create files with content.
2
Stage only file1.txt
Use git add file1.txt to stage only file1.txt for the next commit.
Git
Hint
Use git add with the exact filename file1.txt.
3
Commit the staged file
Use git commit -m "Add file1.txt only" to commit the staged file1.txt with the message exactly as shown.
Git
Hint
Use git commit with the -m flag and the exact commit message.
4
Check commit history to confirm
Run git log --oneline and verify the latest commit message is Add file1.txt only. This confirms only file1.txt was committed.
Git
Hint
The output of git log --oneline should show the commit message Add file1.txt only at the top.
Practice
(1/5)
1. Why is staging changes before committing important in Git?
easy
A. It automatically pushes changes to the remote repository.
B. It lets you choose which changes to include in the next commit.
C. It deletes untracked files from the working directory.
D. It merges branches without conflicts.
Solution
Step 1: Understand the role of staging
Staging allows you to select specific changes to include in your next commit, rather than committing all changes at once.
Step 2: Compare staging with other Git actions
Staging does not push changes, delete files, or merge branches; it only prepares changes for commit.
Final Answer:
It lets you choose which changes to include in the next commit. -> Option B
Quick Check:
Staging = Select changes before commit [OK]
Hint: Staging = picking changes to commit, not pushing or deleting [OK]
Common Mistakes:
Confusing staging with pushing changes
Thinking staging deletes files
Believing staging merges branches
2. Which Git command correctly stages a file named index.html?
easy
A. git add index.html
B. git push index.html
C. git commit index.html
D. git status index.html
Solution
Step 1: Identify the command to stage files
The git add command is used to stage files before committing.
Step 2: Verify other commands' purposes
git commit records changes, git push sends commits to remote, and git status shows current status; none stage files.
Final Answer:
git add index.html -> Option A
Quick Check:
Stage file = git add [OK]
Hint: Use 'git add' to stage files before commit [OK]
The first echo creates file.txt with 'Hello'. Then git add stages this version.
Step 2: Changes after staging are not included
Appending 'World' happens after staging, so this change is not in the commit.
Final Answer:
Only 'Hello' line in file.txt -> Option C
Quick Check:
Commit = staged snapshot, later edits excluded [OK]
Hint: Commit includes only staged changes, not later edits [OK]
Common Mistakes:
Assuming commit includes all current file content
Ignoring that staging freezes file state
Thinking commit auto-stages changes
4. You staged a file with git add app.js but accidentally modified it afterward. What should you do to include the latest changes in your commit?
medium
A. Run git commit immediately
B. Run git reset app.js to unstage
C. Run git push to update remote
D. Run git add app.js again
Solution
Step 1: Recognize staging snapshot behavior
Staging captures the file state at the time of git add. Later edits are not staged automatically.
Step 2: Stage the updated file again
To include the latest changes, you must run git add app.js again to update the staging area.
Final Answer:
Run git add app.js again -> Option D
Quick Check:
Restage after edits to update commit content [OK]
Hint: Restage files after edits before committing [OK]
Common Mistakes:
Committing without restaging changes
Pushing before committing
Unstaging instead of restaging
5. You have modified three files: index.html, style.css, and script.js. You want to commit only index.html and script.js changes but not style.css. Which sequence of commands achieves this?