Skip to content

๐Ÿ“ Chapter 5: File States Explained โ€” Git's "Lifecycle" โ€‹

The Four States of Files โ€‹

In the world of Git, every file has four possible states, like a person's daily routine โ€”

text
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     git add      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    git commit   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚ Untracked โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’  โ”‚ Staged   โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’  โ”‚Committed โ”‚
  โ”‚           โ”‚                  โ”‚          โ”‚                 โ”‚          โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ†‘                            โ”‚                              โ”‚
       โ”‚         Edit file          โ”‚                              โ”‚
       โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                              โ”‚
       โ”‚    โ”‚                                                      โ”‚
       โ”‚    โ–ผ                                                      โ”‚
       โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          Modify committed file              โ”‚
       โ”‚  โ”‚ Modified  โ”‚ โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚  โ”‚           โ”‚
       โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚       โ”‚
       โ”‚       โ”‚ git restore / git checkout
       โ”‚       โ–ผ
       โ”‚    (Discard changes)
       โ”‚
       โ”‚  git rm --cached
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  (Becomes untracked)
StateMeaningReal-life Analogy
UntrackedNew file that Git doesn't know about yetFresh fruit on the counter, not yet in the fridge
ModifiedA tracked file has been changedDish taken out of the fridge with the recipe modified
StagedChanges placed in the staging area, waiting to be committedDish plated, ready to be served
CommittedSafely stored in the Git databaseFinished product archived, save point created

Hands-on Demo: File State Transitions โ€‹

bash
# Scenario: developing "My First Website"

# 1. Create a new file โ€” Untracked
echo 'body { color: blue; }' > style.css
git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        style.css
nothing added to commit but untracked files present

# 2. Add to staging area โ€” Staged
git add style.css
git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   style.css

# 3. Commit โ€” Committed
git commit -m "feat: add base styles"
[main b2c3d4e] feat: add base styles
 1 file changed, 1 insertion(+)
 create mode 100644 style.css

# 4. Modify a committed file โ€” Modified
echo 'body { color: red; font-size: 16px; }' > style.css
git status
On branch 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:   style.css

.gitignore โ€” Tell Git "Don't Track These" โ€‹

Some files you don't want Git to track, like build artifacts, log files, password configs, etc. This is where the .gitignore file comes in.

bash
# Create .gitignore file
cat > .gitignore << 'EOF'
# Dependency directories
node_modules/
vendor/

# Build artifacts
dist/
build/
*.o
*.pyc

# Logs
*.log
logs/

# OS files
.DS_Store
Thumbs.db

# IDE files
.vscode/
.idea/
*.swp

# Environment variables (never commit passwords!)
.env
.env.local
EOF

โš ๏ธ Warning: There's a big pitfall here that I learned the hard way โ€” if a file is already tracked by Git, adding it to .gitignore won't work! You need to first remove it from Git's tracking: git rm --cached &lt;file&gt;. This is especially important for .env files โ€” if you accidentally committed a password, even if you delete it later, it can still be found in the history. Always create .gitignore at the very beginning of your project!