Bird
Raised Fist0
Gitdevops~5 mins

Clean vs dirty working directory in Git - CLI Comparison

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 Git, your project files can be in two main states: clean or dirty. A clean working directory means all your changes are saved and tracked, while a dirty one means you have changes not yet saved or tracked. Understanding this helps you avoid losing work or committing unfinished changes.
When you want to check if you have unsaved changes before committing.
When you want to make sure your project is ready to share with others.
When you want to avoid committing temporary or incomplete changes.
When you want to clean up your project before switching to another task.
When you want to confirm that your last commit included all your changes.
Commands
This command shows the current state of your working directory and staging area. It tells you if your directory is clean or dirty by listing changes not yet committed.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
This command adds a new line to the README.md file, creating a change in your working directory that makes it dirty.
Terminal
echo "New line" >> README.md
Expected OutputExpected
No output (command runs silently)
Run git status again to see that your working directory is now dirty because of the change in README.md.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
This command stages the change in README.md, preparing it to be committed. The working directory is still dirty until you commit.
Terminal
git add README.md
Expected OutputExpected
No output (command runs silently)
This command commits the staged changes, making the working directory clean again because all changes are saved in Git.
Terminal
git commit -m "Update README with new line"
Expected OutputExpected
[main abcdef1] Update README with new line 1 file changed, 1 insertion(+)
-m - Adds a commit message inline without opening an editor
Check again to confirm the working directory is clean after the commit.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Key Concept

If you remember nothing else from this pattern, remember: a clean working directory means all your changes are saved and tracked, while a dirty one means you have unsaved or untracked changes.

Common Mistakes
Trying to commit without staging changes first
Git will not commit changes that are not staged, so your changes remain uncommitted and the working directory stays dirty.
Use 'git add <file>' to stage changes before committing.
Ignoring 'git status' output and committing incomplete changes
You might commit unfinished or unwanted changes, causing confusion or errors later.
Always check 'git status' to review changes before committing.
Assuming the working directory is clean without checking
You might think all changes are saved when some are not, risking data loss.
Run 'git status' regularly to confirm the state of your working directory.
Summary
Use 'git status' to check if your working directory is clean or dirty.
Make changes to files to create a dirty working directory.
Stage changes with 'git add' and commit them with 'git commit' to clean the directory.

Practice

(1/5)
1. What does it mean when your Git working directory is described as clean?
easy
A. There are conflicts from a merge.
B. There are untracked files present.
C. There are changes staged but not committed.
D. There are no changes to commit; all files are saved in Git.

Solution

  1. Step 1: Understand the meaning of a clean working directory

    A clean working directory means no changes are pending to be committed or staged.
  2. Step 2: Compare with other states

    Untracked files, staged changes, or conflicts mean the directory is dirty, not clean.
  3. Final Answer:

    There are no changes to commit; all files are saved in Git. -> Option D
  4. Quick Check:

    Clean working directory = no uncommitted changes [OK]
Hint: Clean means no changes to commit or stage [OK]
Common Mistakes:
  • Confusing staged changes with clean state
  • Thinking untracked files mean clean
  • Assuming conflicts mean clean
2. Which Git command correctly shows the current state of your working directory?
easy
A. git push origin main
B. git commit -m "status"
C. git status
D. git checkout

Solution

  1. Step 1: Identify the command to check working directory state

    The command git status shows staged, unstaged, and untracked changes.
  2. Step 2: Eliminate other commands

    git commit saves changes, git push uploads commits, git checkout switches branches or files.
  3. Final Answer:

    git status -> Option C
  4. Quick Check:

    Check working directory state = git status [OK]
Hint: Use 'git status' to see working directory changes [OK]
Common Mistakes:
  • Using git commit to check status
  • Confusing git push with status check
  • Using git checkout incorrectly
3. You run git status and see:
Changes not staged for commit:
modified: app.js

What is the state of your working directory?
medium
A. Dirty working directory with unstaged changes
B. Dirty working directory with staged changes
C. Clean working directory
D. Detached HEAD state

Solution

  1. Step 1: Interpret the git status output

    The message "Changes not staged for commit" means files are modified but not added to staging.
  2. Step 2: Determine working directory state

    Unstaged changes mean the directory is dirty, not clean, and changes are not staged.
  3. Final Answer:

    Dirty working directory with unstaged changes -> Option A
  4. Quick Check:

    Unstaged changes = dirty directory [OK]
Hint: Unstaged changes mean dirty directory [OK]
Common Mistakes:
  • Confusing unstaged with staged changes
  • Assuming clean when files are modified
  • Mixing detached HEAD with working directory state
4. You see this output after running 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?
medium
A. Run git reset HEAD index.html to unstage changes
B. Run git commit to save changes
C. Run git add index.html again
D. Run git checkout index.html to stage changes

Solution

  1. Step 1: Understand the meaning of staged changes

    "Changes to be committed" means files are staged but not committed, so directory is dirty.
  2. Step 2: Unstage changes to check clean state

    Running git reset HEAD index.html unstages the file, showing if working directory has unstaged changes.
  3. Final Answer:

    Run git reset HEAD index.html to unstage changes -> Option A
  4. Quick Check:

    Unstage changes to check clean state [OK]
Hint: Unstage files with git reset HEAD to check clean state [OK]
Common Mistakes:
  • Adding files again instead of unstaging
  • Committing without checking unstaged changes
  • Using git checkout to stage files (wrong)
5. You modified two files: app.py and README.md. You staged app.py but not README.md. What will git status show?
hard
A. No changes to commit, working directory clean
B. Changes to be committed: app.py; Changes not staged for commit: README.md
C. Changes not staged for commit: app.py and README.md
D. Untracked files: app.py and README.md

Solution

  1. Step 1: Identify staged and unstaged files

    app.py is staged, so it appears under "Changes to be committed".
  2. Step 2: Identify unstaged files

    README.md is modified but not staged, so it appears under "Changes not staged for commit".
  3. Final Answer:

    Changes to be committed: app.py; Changes not staged for commit: README.md -> Option B
  4. Quick Check:

    Staged vs unstaged files shown separately [OK]
Hint: Staged files show as 'to be committed', unstaged as 'not staged' [OK]
Common Mistakes:
  • Assuming all modified files are staged
  • Confusing untracked with modified files
  • Thinking working directory is clean with staged changes