Bird
Raised Fist0
Gitdevops~5 mins

How files move between three areas in Git - Step-by-Step CLI Walkthrough

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
Introduction
When you work with files in Git, they move through three main areas: the working directory, the staging area, and the repository. This process helps you control which changes are saved and shared.
When you want to prepare specific changes before saving them permanently.
When you want to check which files are ready to be saved and which are still being worked on.
When you want to undo changes before saving them.
When you want to organize your work into small, clear steps.
When you want to share your changes with others after confirming they are ready.
Commands
This command shows the current state of your files: which are changed but not saved, which are staged and ready to be saved, and which are already saved in the repository.
Terminal
git status
Expected OutputExpected
On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) example.txt nothing added to commit but untracked files present (use "git add" to track)
This command moves the file 'example.txt' from the working directory to the staging area, marking it ready to be saved in the next commit.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Check again to see that 'example.txt' is now staged and ready to be saved.
Terminal
git status
Expected OutputExpected
On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: example.txt
This command saves the staged changes permanently in the repository with a message describing the change.
Terminal
git commit -m "Add example.txt file"
Expected OutputExpected
[main (root-commit) abcdef1] Add example.txt file 1 file changed, 1 insertion(+), 0 deletions(-) create mode 100644 example.txt
-m - Adds a commit message directly in the command
Finally, check that there are no changes left to save and the working directory is clean.
Terminal
git status
Expected OutputExpected
On branch main nothing to commit, working tree clean
Key Concept

If you remember nothing else from this pattern, remember: files move from working directory to staging area with 'git add', then from staging area to repository with 'git commit'.

Common Mistakes
Trying to commit changes without adding them first
Git only saves changes that are staged; unstaged changes are ignored in the commit.
Use 'git add <file>' to stage changes before committing.
Assuming 'git add' saves changes permanently
'git add' only stages changes; they are not saved in the repository until you commit.
After 'git add', run 'git commit' to save changes permanently.
Not checking 'git status' to see file states
Without checking, you might miss which files are staged or unstaged, causing confusion.
Use 'git status' often to understand the current state of your files.
Summary
Use 'git add' to move files from the working directory to the staging area.
Use 'git commit' to save staged files permanently in the repository.
Use 'git status' to check the state of files in all three areas.

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

  1. 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.
  2. 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.
  3. Final Answer:

    git add -> Option A
  4. 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

  1. Step 1: Identify the commit command

    git commit -m "message" saves the staged files into the local repository with a message.
  2. Step 2: Understand other commands

    git add . stages files, git status shows status, and git init creates a new repo.
  3. Final Answer:

    git commit -m "Save changes" -> Option A
  4. Quick Check:

    Save staged files = git commit [OK]
Hint: Commit staged files with git commit -m [OK]
Common Mistakes:
  • Using git add instead of git commit to save
  • Confusing git status with commit
  • Trying to commit without staging files
3. Given these commands run in order:
echo "Hello" > file.txt
git add file.txt
git commit -m "Add file"

Where is file.txt after these commands?
medium
A. Only in the working directory
B. In the local repository and working directory
C. Deleted from all areas
D. In the staging area only

Solution

  1. Step 1: Create and stage the file

    The file is created in the working directory, then moved to the staging area by git add.
  2. Step 2: Commit the file

    git commit saves the staged file to the local repository. The file remains in the working directory.
  3. Final Answer:

    In the local repository and working directory -> Option B
  4. 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

  1. Step 1: Understand the message meaning

    "Nothing to commit" means no changes are staged for commit.
  2. Step 2: Identify missing staging step

    If you forgot git add, no files are staged, so commit has nothing to save.
  3. Final Answer:

    You forgot to stage files with git add -> Option C
  4. 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

  1. Step 1: Stage the specific file

    Use git add app.js to move only app.js to the staging area.
  2. Step 2: Commit the staged file

    Run git commit -m "Save app.js" to save the staged changes to the local repository.
  3. Final Answer:

    git add app.js then git commit -m "Save app.js" -> Option D
  4. Quick Check:

    Stage then commit specific file = git add + git commit [OK]
Hint: Add specific file before commit to save only it [OK]
Common Mistakes:
  • Committing before adding files
  • Using git push instead of commit
  • Adding all files instead of just one