Now that you have Git installed and your first repository created, it's time to understand how Git tracks files and manages changes. This tutorial follows our real terminal session, explaining every file status, staging operation, and commit with complete detail for absolute beginners.
🎯 What You'll Learn: In this hands-on tutorial, you'll discover:
- The complete Git file status system (untracked, tracked, modified, staged)
- What the staging area is and why it exists
- How to add files to Git tracking with detailed command explanations
- Making your first commits with meaningful messages
- Understanding Git log output and commit history
- Real-world workflow patterns for daily development
- Every Git status message explained line by line Prerequisites: Git installed and first repository created (Part 3)
📊 Understanding Git File Status System
The Four File States in Git
Before we dive into our terminal session, let's understand the four possible states any file can be in within a Git repository:
File Status | Description | Git's Knowledge | Next Action |
---|---|---|---|
Untracked | File exists but Git doesn't know about it | Not watching this file | Use git add to start tracking |
Tracked (Unmodified) | File is tracked and hasn't changed | Knows this file, no changes detected | Nothing - file is up to date |
Modified | File is tracked and has been changed | Knows file changed since last commit | Use git add to stage changes |
Staged | Changes are ready to be committed | Changes prepared for next snapshot | Use git commit to save snapshot |
Visual File Status Flow
File Lifecycle in Git:
New File → [git add] → Staged → [git commit] → Tracked (Unmodified)
↑ ↓
[git add] [edit file]
↑ ↓
Modified ← ← ← ← ← ← ← ← ← ← ← ← ← ←
Key Insight: Files move between these states as you work. Understanding this flow is crucial for effective Git usage.
📁 Creating Your First Files
Let's start by creating some files in our repository and watch how Git reacts:
Creating the First File
[centos9@vbox myproject 16:45:48]$ touch first.txt
[centos9@vbox myproject 16:46:37]$ nano first.txt
[centos9@vbox myproject 16:46:45]$ cat first.txt
hello
Command Breakdown:
touch first.txt
- Creates an empty file named "first.txt"nano first.txt
- Opens the file in the nano text editorcat first.txt
- Displays the contents of the file
What happened: We created a file with the content "hello". Git doesn't know about this file yet.
Checking Git Status After File Creation
[centos9@vbox myproject 16:46:58]$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
first.txt
nothing added to commit but untracked files present (use "git add" to track)
Understanding the New Git Status Output
Status Line | Meaning | What Git Is Telling You |
---|---|---|
"Untracked files:" | Git found files it doesn't track | "I see new files but I'm not watching them" |
"use "git add <file>..."" | Instruction to start tracking | "If you want me to track these, use git add" |
"first.txt" | Specific untracked file | "This is the file I'm not tracking" |
"nothing added to commit" | No changes staged for commit | "No files ready to be saved in a snapshot" |
Adding a Second File
Let's create another file to see how Git handles multiple untracked files:
[centos9@vbox myproject 16:47:01]$ touch second
[centos9@vbox myproject 16:47:45]$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
first.txt
second
nothing added to commit but untracked files present (use "git add" to track)
Key Observation: Git now shows both untracked files. Git detects all files in your working directory and reports their status.
📋 Understanding the Staging Area
What is the Staging Area?
The Staging Area (also called "Index") is like a preparation area for your next commit. Think of it as:
Photo Album Analogy:
- Working Directory = Photos scattered on your desk
- Staging Area = Photos you've selected and arranged for the next album page
- Repository = The completed album pages (commits)
Why does the staging area exist?
- ✅ Selective Commits: Choose exactly which changes to include
- ✅ Review Changes: Double-check what you're about to commit
- ✅ Organize Work: Group related changes together
- ✅ Flexibility: Stage some files, continue working on others
Adding Files to Staging Area
Let's add our files to the staging area:
[centos9@vbox myproject 16:47:48]$ git add .
[centos9@vbox myproject 16:48:32]$ # or "git add first.txt second"
Command Options Explained:
git add .
- Add all files in current directory and subdirectoriesgit add first.txt second
- Add specific files by namegit add *.txt
- Add all files matching a pattern (all .txt files)git add --all
orgit add -A
- Add all files in the repository
Checking Status After Staging
[centos9@vbox myproject 16:49:15]$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: first.txt
new file: second
Understanding Staged File Status
Status Change | Before git add | After git add |
---|---|---|
Section Header | "Untracked files:" | "Changes to be committed:" |
File Status | Listed as untracked | "new file:" prefix shows they're staged |
Next Action | "use git add" to track | "git rm --cached" to unstage |
Git's Understanding | "I don't know these files" | "These files are ready for the next commit" |
💾 Making Your First Commit
What is a Commit?
A commit is like taking a snapshot of your project at a specific moment. Each commit includes:
Every Commit Contains:
- 📸 Snapshot: Complete state of all staged files
- 👤 Author: Who made this commit (from git config)
- 📅 Timestamp: Exactly when the commit was made
- 📝 Message: Description of what changed and why
- 🔗 Parent: Link to previous commit (building the timeline)
- 🔢 Hash: Unique identifier for this exact commit
Creating Your First Commit
[centos9@vbox myproject 16:49:19]$ git commit -m "added files first.txt and second"
[master (root-commit) c9c6bf3] added files first.txt and second
2 files changed, 1 insertion(+)
create mode 100644 first.txt
create mode 100644 second
Breaking Down the Commit Command
Command Part | Purpose | Explanation |
---|---|---|
git commit | Create a commit from staged changes | Takes a snapshot of all staged files |
-m | Add message directly on command line | Avoids opening text editor for message |
"added files..." | Commit message describing changes | Should be clear and descriptive |
Understanding Git's Commit Response
Let's decode every part of Git's response:
Output Part | Value | Meaning |
---|---|---|
[master | master | Commit was made on the master branch |
(root-commit) | root-commit | This is the very first commit (no parent) |
c9c6bf3] | c9c6bf3 | Short hash (first 7 chars of unique commit ID) |
2 files changed | 2 | Number of files affected by this commit |
1 insertion(+) | 1 | Number of lines added (the "hello" line) |
create mode 100644 | 100644 | File permissions (regular file, readable/writable) |
Checking Status After Commit
[centos9@vbox myproject 16:50:12]$ git status
On branch master
nothing to commit, working tree clean
What This Means:
- "nothing to commit": All changes have been saved in the commit
- "working tree clean": No modified files, everything matches the last commit
- No "No commits yet": We now have commit history!
📜 Exploring Git Log
Viewing Commit History
[centos9@vbox myproject 16:50:17]$ git log
commit c9c6bf38440b6bd55fd5d32f8dc37a4b079ce012 (HEAD -> master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Sep 22 16:50:12 2025 +0500
added files first.txt and second
Understanding Git Log Output
Log Element | Value | Purpose |
---|---|---|
commit | c9c6bf38440b6bd55fd5d32f8dc37a4b079ce012 | Full unique hash for this commit |
(HEAD -> master) | Current position | You are here, on master branch |
Author | Owais Abbasi <owais.abbasi9@gmail.com> | Who made this commit (from git config) |
Date | Mon Sep 22 16:50:12 2025 +0500 | Exact timestamp with timezone |
Message | added files first.txt and second | Description of what this commit does |
🔄 Working with Modified Files
Now let's see what happens when we modify existing files:
Modifying an Existing File
[centos9@vbox myproject 16:50:42]$ nano first.txt
[centos9@vbox myproject 16:51:27]$ cat first.txt
hello
working
What we did: Added a new line "working" to first.txt file.
Git Status After File Modification
[centos9@vbox myproject 16:51:29]$ git status
On branch master
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: first.txt
no changes added to commit (use "git add" and/or "git commit -a")
Understanding Modified File Status
Status Element | Meaning | What Git Knows |
---|---|---|
"Changes not staged" | File has been modified but not added to staging | "I see changes but they're not ready to commit" |
"modified: first.txt" | Specific file that has changes | "This file is different from last commit" |
"git restore" | Option to undo changes | "You can undo these changes if you want" |
"no changes added to commit" | Nothing in staging area | "Nothing ready for next commit yet" |
Multiple Modified Files
Let's modify the second file too:
[centos9@vbox myproject 16:51:35]$ nano second
[centos9@vbox myproject 16:52:35]$ cat second
working
[centos9@vbox myproject 16:52:36]$ git status
On branch master
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: first.txt
modified: second
no changes added to commit (use "git add" and/or "git commit -a")
Key Observation: Git tracks all modified files and lists them separately. Each file moves through the status system independently.
🎯 Selective Staging and Committing
Staging Only One File
Let's demonstrate selective staging by adding only one file:
[centos9@vbox myproject 16:52:38]$ git add first.txt
[centos9@vbox myproject 16:52:53]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: first.txt
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: second
Understanding Mixed Status
This status output shows a crucial Git concept: files can be in different states simultaneously:
File | Status | What This Means | Next Action |
---|---|---|---|
first.txt | Staged (ready to commit) | Changes will be included in next commit | Can commit now or stage more files |
second | Modified (not staged) | Changes exist but won't be in next commit | Use git add to stage, or leave for later |
Committing Staged Changes
[centos9@vbox myproject 16:52:57]$ git commit -m "update first.txt"
[master 4d0a85e] update first.txt
1 file changed, 2 insertions(+)
Important: Only the staged file (first.txt) was committed. The modified second file remains in the working directory.
Status After Partial Commit
[centos9@vbox myproject 16:53:49]$ git status
On branch master
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: second
no changes added to commit (use "git add" and/or "git commit -a")
Key Insight: After committing first.txt, it's no longer shown as modified. Only the unstaged file (second) appears in the status.
📜 Viewing Complete Git History
Let's look at our commit history with multiple commits:
[centos9@vbox myproject 16:53:53]$ git log
commit 4d0a85ecd7b063d5c20089e754577481b07772f4 (HEAD -> master)
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Sep 22 16:53:49 2025 +0500
update first.txt
commit c9c6bf38440b6bd55fd5d32f8dc37a4b079ce012
Author: Owais Abbasi <owais.abbasi9@gmail.com>
Date: Mon Sep 22 16:50:12 2025 +0500
added files first.txt and second
Understanding Git Log Timeline
Commit Order | Commit Hash | What Happened | Status |
---|---|---|---|
Latest (top) | 4d0a85e... | Updated first.txt file | (HEAD -> master) Current position |
Previous | c9c6bf3... | Initial commit with both files | Parent of current commit |
Key Points:
- Newest commits appear first (reverse chronological order)
- HEAD -> master shows where you currently are
- Each commit has unique hash for precise identification
- Complete timeline of your project's evolution
🎯 Git Workflow Summary
The Complete Git Workflow
Daily Git Workflow:
- Create/Edit Files → Files are untracked or modified
git status
→ See what files have changedgit add <files>
→ Move files to staging areagit status
→ Verify what's staged for commitgit commit -m "message"
→ Create snapshot with meaningful messagegit log
→ Review your commit history- Repeat → Continue developing with confidence
Best Practices for Commit Messages
Quality | Example | Why |
---|---|---|
Good | "Add user authentication feature" | Clear, describes what was added |
Good | "Fix login bug for mobile users" | Explains what was fixed and for whom |
Poor | "stuff" | Too vague, no useful information |
Poor | "changed file" | Doesn't explain what changed or why |
📋 Git File Status & Workflow Quick Reference Cheat Sheet
🚀 Essential Git Commands
# Check repository status
git status
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all files in current directory
git add *.txt # Add all .txt files
# Commit changes
git commit -m "Your message" # Commit with message
git commit # Open editor for commit message
# View commit history
git log # Full commit history
git log --oneline # Compact one-line format
📊 File Status System Reference
Status | What It Means | Git Status Shows | Next Action |
---|---|---|---|
Untracked | New file Git doesn't know about | "Untracked files: filename.txt" | git add filename.txt |
Modified | Tracked file has changes | "modified: filename.txt" | git add filename.txt |
Staged | Changes ready to commit | "new file: filename.txt" or "modified: filename.txt" | git commit -m "message" |
Committed | Changes saved in repository | "working tree clean" | Continue working |
🔄 Complete Workflow Patterns
Pattern 1: Adding New Files
# 1. Create new file
touch newfile.txt
echo "content" > newfile.txt
# 2. Check status (will show untracked)
git status
# 3. Add to staging area
git add newfile.txt
# 4. Check status (will show staged)
git status
# 5. Commit
git commit -m "Add new file with initial content"
# 6. Verify (should show clean working tree)
git status
Pattern 2: Modifying Existing Files
# 1. Edit existing file
echo "new content" >> existing.txt
# 2. Check status (will show modified)
git status
# 3. Add changes to staging
git add existing.txt
# 4. Commit changes
git commit -m "Update existing file with new content"
Pattern 3: Working with Multiple Files
# 1. Modify multiple files
echo "content1" > file1.txt
echo "content2" > file2.txt
echo "content3" > file3.txt
# 2. Check what's changed
git status
# 3. Stage specific files
git add file1.txt file2.txt # Add specific files
# OR
git add . # Add all files
# 4. Commit with descriptive message
git commit -m "Add three new configuration files"
🎯 Git Status Output Decoder
Git Status Message | What It Means | What To Do |
---|---|---|
"On branch master" | Shows current branch name | Information only |
"No commits yet" | Repository has no commit history | Make your first commit |
"Changes to be committed:" | Files in staging area | Ready to commit |
"Changes not staged:" | Modified files not yet staged | Use git add to stage |
"Untracked files:" | New files Git doesn't track | Use git add to start tracking |
"working tree clean" | No changes detected | Nothing to do |
💡 Command Variations & Tips
Command | What It Does | When To Use |
---|---|---|
git add . | Add all files in current directory | When you want to stage everything |
git add *.txt | Add all .txt files | When staging specific file types |
git add file1 file2 | Add multiple specific files | For selective staging |
git log --oneline | Compact commit history | Quick overview of commits |
git log -n 5 | Show last 5 commits | Limit log output |
git status -s | Short status format | Quick status check |
📝 Commit Message Best Practices
Good Commit Message Format
# Pattern: [Type]: [Brief description]
git commit -m "Add: user authentication system"
git commit -m "Fix: login validation bug"
git commit -m "Update: README with installation steps"
git commit -m "Remove: deprecated configuration file"
Message Type Examples
Type | Use When | Example |
---|---|---|
Add | Creating new files or features | "Add contact form validation" |
Fix | Correcting bugs or issues | "Fix mobile navigation menu" |
Update | Modifying existing functionality | "Update API endpoint responses" |
Remove | Deleting files or features | "Remove unused CSS files" |
Refactor | Code cleanup without changing functionality | "Refactor user service methods" |
🚨 Common Scenarios & Solutions
Scenario 1: Accidentally Staged Wrong File
# Problem: Added wrong file to staging
git add wrong-file.txt
# Solution: Remove from staging (keeps file changes)
git reset wrong-file.txt
# Then add the correct file
git add correct-file.txt
Scenario 2: Want to See What Changed
# See what files changed (not staged yet)
git diff
# See what files are staged for commit
git diff --staged
# See specific file changes
git diff filename.txt
Scenario 3: Check Specific Commit Details
# Show detailed info about latest commit
git show
# Show specific commit (replace with actual hash)
git show 4d0a85e
# Show files changed in commit
git show --name-only 4d0a85e
🎯 Daily Workflow Checklist
Before Starting Work:
- [ ]
git status
- Check current state - [ ]
git log --oneline -5
- Review recent commits
During Development:
- [ ] Create/edit files as needed
- [ ]
git status
- See what changed - [ ]
git add <files>
- Stage your changes - [ ]
git status
- Verify staging area - [ ]
git commit -m "Clear message"
- Save snapshot
After Commits:
- [ ]
git log --oneline
- Verify commit was created - [ ]
git status
- Confirm clean working tree
🔧 Troubleshooting Quick Fixes
Problem | Symptoms | Solution |
---|---|---|
Nothing to commit | "working tree clean" | Make some changes first, then git add |
Files not included in commit | Still showing as modified | Use git add before git commit |
Commit message editor opens | Text editor instead of command line | Always use git commit -m "message" |
Too many files to add individually | Many untracked files | Use git add . to add all |
🎯 Key Takeaways
✅ Essential Concepts Mastered
-
File Status System: Understood untracked, modified, staged, and committed states
-
Staging Area: Learned why it exists and how to use it for selective commits
-
Git Commands: Mastered
git add
,git commit
,git status
, andgit log
-
Workflow Pattern: Create → Add → Commit → Repeat with meaningful messages
-
Selective Commits: Can stage some files while leaving others for later
-
History Tracking: Git log shows complete timeline with author, date, and messages
-
Status Interpretation: Can read and understand Git's status messages
🎉 Congratulations! You now understand the fundamental Git workflow and can:
- ✅ Track files and understand their status in Git
- ✅ Use the staging area to prepare commits selectively
- ✅ Create meaningful commits with descriptive messages
- ✅ View and interpret your project's commit history
- ✅ Work with modified files and understand Git's feedback
- ✅ Follow a professional Git workflow for daily development
You have the foundation for effective version control in any project!
💬 Discussion
How was your experience with the Git workflow?
- Which part of the file status system was most confusing initially?
- Did the staging area concept make sense with the photo album analogy?
- Have you tried the selective staging workflow in your own projects?
- What challenges do you face with writing good commit messages?
- Do you understand why Git shows so much information in its status output?