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
How files move between three areas in Git
📖 Scenario: You are working on a simple project using Git. You want to understand how files move between the three main areas in Git: the working directory, the staging area (index), and the repository (commit history).
🎯 Goal: Learn how to create a file, add it to the staging area, and commit it to the Git repository. This will help you understand the flow of files in Git.
📋 What You'll Learn
Create a new file called example.txt with some text
Add the file example.txt to the Git staging area using git add
Commit the staged file with the message Initial commit of example.txt
Show the commit log to verify the commit
💡 Why This Matters
🌍 Real World
Understanding how files move between the working directory, staging area, and repository is essential for managing changes in any software project using Git.
💼 Career
This knowledge is fundamental for developers, DevOps engineers, and anyone working with version control systems to collaborate and track code changes effectively.
Progress0 / 4 steps
1
Create a new file in the working directory
Create a file called example.txt in your working directory with the content Hello, Git! using the command echo "Hello, Git!" > example.txt.
Git
Hint
Use the echo command to write text into a file.
2
Add the file to the Git staging area
Use the command git add example.txt to add the file example.txt to the Git staging area.
Git
Hint
The git add command moves files from the working directory to the staging area.
3
Commit the staged file to the repository
Commit the staged file using the command git commit -m "Initial commit of example.txt" to save it in the Git repository.
Git
Hint
The git commit command saves the staged changes to the repository with a message.
4
Show the commit log
Use the command git log --oneline to display the commit history and verify your commit.
Git
Hint
The git log --oneline command shows a short summary of commits.
Practice
(1/5)
1. Which command moves files from the working directory to the staging area in Git?
easy
A. git add
B. git commit
C. git push
D. git clone
Solution
Step 1: Understand the role of git add
The git add command moves files from the working directory to the staging area, preparing them for commit.
Step 2: Differentiate from other commands
git commit saves staged files to the local repository, git push sends commits to a remote, and git clone copies a repository.
Final Answer:
git add -> Option A
Quick Check:
Staging area update = git add [OK]
Hint: Add files to staging with git add [OK]
Common Mistakes:
Confusing git add with git commit
Thinking git push moves files locally
Using git clone to stage files
2. Which of these commands correctly saves staged files to the local repository?
easy
A. git commit -m "Save changes"
B. git status
C. git add .
D. git init
Solution
Step 1: Identify the commit command
git commit -m "message" saves the staged files into the local repository with a message.
Step 2: Understand other commands
git add . stages files, git status shows status, and git init creates a new repo.
The file is created in the working directory, then moved to the staging area by git add.
Step 2: Commit the file
git commit saves the staged file to the local repository. The file remains in the working directory.
Final Answer:
In the local repository and working directory -> Option B
Quick Check:
Commit saves staged files, working files remain [OK]
Hint: Committed files stay in repo and working directory [OK]
Common Mistakes:
Thinking commit removes file from working directory
Believing staged files disappear after commit
Confusing staging area with repository
4. You ran git commit -m "Update" but Git says "nothing to commit". What is the likely cause?
medium
A. You committed files but did not push
B. You are in the wrong directory
C. You forgot to stage files with git add
D. You need to run git init again
Solution
Step 1: Understand the message meaning
"Nothing to commit" means no changes are staged for commit.
Step 2: Identify missing staging step
If you forgot git add, no files are staged, so commit has nothing to save.
Final Answer:
You forgot to stage files with git add -> Option C
Quick Check:
Stage files before commit = git add [OK]
Hint: Stage files first with git add before commit [OK]
Common Mistakes:
Thinking commit auto-stages files
Confusing push with commit
Assuming git init fixes this error
5. You edited app.js in your working directory. You want to save only this file to the local repository without including other changes. Which sequence of commands correctly moves app.js through Git's three areas?
hard
A. git add . then git commit -m "Save app.js"
B. git commit -m "Save app.js" then git add app.js
C. git push then git add app.js
D. git add app.js then git commit -m "Save app.js"
Solution
Step 1: Stage the specific file
Use git add app.js to move only app.js to the staging area.
Step 2: Commit the staged file
Run git commit -m "Save app.js" to save the staged changes to the local repository.
Final Answer:
git add app.js then git commit -m "Save app.js" -> Option D
Quick Check:
Stage then commit specific file = git add + git commit [OK]
Hint: Add specific file before commit to save only it [OK]