0
0
Gitdevops~5 mins

Clean vs dirty working directory in Git - CLI Comparison

Choose your learning style9 modes available
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.