Ever sent unfinished work because you didn't know what files were changed?
Clean vs dirty working directory in Git - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are working on a group project and you have many files open and changed on your computer. You want to share your work, but you are not sure which files you have changed and which are still the original ones.
Manually checking each file to see if it has changed is slow and confusing. You might forget to save some changes or accidentally share unfinished work. This can cause mistakes and waste time fixing problems later.
Git shows you if your working directory is clean or dirty. A clean directory means all changes are saved and ready. A dirty directory means you have unsaved or untracked changes. This helps you know exactly what needs attention before sharing.
open each file and check for changes
git status
This lets you confidently manage your work and avoid errors by knowing the exact state of your files at any time.
Before sending your project to a teammate, you run git status to see if you forgot to save or add any files. This prevents sending incomplete or broken work.
Manual file checks are slow and error-prone.
Git tells you if your working directory is clean or dirty.
This helps you manage changes safely and efficiently.
Practice
clean?Solution
Step 1: Understand the meaning of a clean working directory
A clean working directory means no changes are pending to be committed or staged.Step 2: Compare with other states
Untracked files, staged changes, or conflicts mean the directory is dirty, not clean.Final Answer:
There are no changes to commit; all files are saved in Git. -> Option DQuick Check:
Clean working directory = no uncommitted changes [OK]
- Confusing staged changes with clean state
- Thinking untracked files mean clean
- Assuming conflicts mean clean
Solution
Step 1: Identify the command to check working directory state
The commandgit statusshows staged, unstaged, and untracked changes.Step 2: Eliminate other commands
git commitsaves changes,git pushuploads commits,git checkoutswitches branches or files.Final Answer:
git status -> Option CQuick Check:
Check working directory state = git status [OK]
- Using git commit to check status
- Confusing git push with status check
- Using git checkout incorrectly
git status and see:Changes not staged for commit:
modified: app.js
What is the state of your working directory?
Solution
Step 1: Interpret the git status output
The message "Changes not staged for commit" means files are modified but not added to staging.Step 2: Determine working directory state
Unstaged changes mean the directory is dirty, not clean, and changes are not staged.Final Answer:
Dirty working directory with unstaged changes -> Option AQuick Check:
Unstaged changes = dirty directory [OK]
- Confusing unstaged with staged changes
- Assuming clean when files are modified
- Mixing detached HEAD with working directory state
git status:On branch main
Changes to be committed:
modified: index.html
But you want to check if your working directory is clean. What should you do?
Solution
Step 1: Understand the meaning of staged changes
"Changes to be committed" means files are staged but not committed, so directory is dirty.Step 2: Unstage changes to check clean state
Runninggit reset HEAD index.htmlunstages the file, showing if working directory has unstaged changes.Final Answer:
Run git reset HEAD index.html to unstage changes -> Option AQuick Check:
Unstage changes to check clean state [OK]
- Adding files again instead of unstaging
- Committing without checking unstaged changes
- Using git checkout to stage files (wrong)
app.py and README.md. You staged app.py but not README.md. What will git status show?Solution
Step 1: Identify staged and unstaged files
app.py is staged, so it appears under "Changes to be committed".Step 2: Identify unstaged files
README.md is modified but not staged, so it appears under "Changes not staged for commit".Final Answer:
Changes to be committed: app.py; Changes not staged for commit: README.md -> Option BQuick Check:
Staged vs unstaged files shown separately [OK]
- Assuming all modified files are staged
- Confusing untracked with modified files
- Thinking working directory is clean with staged changes
