0
0
Gitdevops~5 mins

Why three areas matter (working directory, staging, repository) in Git - Why It Works

Choose your learning style9 modes available
Introduction
When you work with files in a project, you need a way to track changes safely. Git uses three areas to help you manage your work: the working directory, the staging area, and the repository. This setup helps you control what changes are saved and shared.
When you want to edit files but only save some changes permanently.
When you want to review your changes before saving them.
When you want to keep a history of your project versions.
When you want to undo changes before saving them.
When you want to share your saved changes with others.
Commands
This command shows the current state of your working directory and staging area. It tells you which files are changed, staged, or untracked.
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: example.txt no changes added to commit (use "git add" and/or "git commit -a")
This command moves the changes in example.txt from the working directory to the staging area. It means you are ready to save these changes.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Run git status again to see that example.txt is now staged and ready to be committed.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: example.txt
This command saves the staged changes into the repository with a message describing the update.
Terminal
git commit -m "Update example.txt with new info"
Expected OutputExpected
[main abc1234] Update example.txt with new info 1 file changed, 3 insertions(+), 1 deletion(-)
-m - Adds a commit message inline without opening an editor
Check the status again to confirm there are no changes left in the working directory or staging area 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: the working directory is where you edit files, the staging area is where you prepare changes, and the repository is where changes are saved permanently.

Common Mistakes
Trying to commit changes without adding them to the staging area first.
Git will not include those changes in the commit because they are not staged.
Use 'git add <file>' to stage changes before committing.
Adding all files without checking what changes are staged.
You might commit unwanted changes or mistakes.
Use 'git status' to review changes before staging and committing.
Editing files and expecting them to be saved in the repository immediately.
Changes stay only in the working directory until staged and committed.
Stage changes with 'git add' and then commit with 'git commit'.
Summary
Use 'git status' to see changes in the working directory and staging area.
Use 'git add' to move changes from working directory to staging area.
Use 'git commit' to save staged changes into the repository.