In Git, files go through a specific lifecycle. They are not simply "saved" or "unsaved". They move through different areas: Untracked, Working Directory, Staging Area, and Repository.
Understanding this lifecycle is the secret to mastering Git. It explains why we have to run add before commit.
- Untracked: A brand new file. Git doesn't know it exists yet.
- Tracked (Modified): A file Git knows about, which has been changed but not added to the Staging Area.
- Staged: A file that is ready to be committed.
- Committed: The file is safely stored in the local Git database.
flowchart LR
A[Untracked / Modified] -->|git add| B[Staged]
B -->|git commit| C[Committed]
C -->|Edit File| A
- Working Directory: Where you type your code.
- Staging Area: Where you prepare files for saving.
- Repository: Where files are permanently saved as commits.
stateDiagram-v2
[*] --> Untracked : Create new file
Untracked --> Staged : git add
Staged --> Committed : git commit
Committed --> Modified : Edit file
Modified --> Staged : git add
Modified --> Committed : git restore (discard changes)
Staged --> Modified : git restore --staged (unstage)