0
0
Gitdevops~5 mins

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

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