DevOps · Infrastructure as Code (IaC) · Git Series – Part 4
Git files are the building blocks of version control. Understanding how Git classifies and tracks different file states is essential for effective repository management.
Understanding how Git classifies files is one of the most important skills for any DevOps engineer or developer. Git treats files differently based on whether they are new, changed, staged, or intentionally ignored.
In this chapter, you’ll learn:
- What Git means by tracked, untracked, and modified
- How files move through the Git lifecycle
- How
.gitignoreworks and how to use it - Real examples & best practices
- Tips to avoid common mistakes
Table of Contents
1. Git File Lifecycle Overview
Every file inside a Git repository falls into one of these categories:
1. Untracked
Files Git does not know about yet.
Example: A new file created after git init.
Tracked Git Files
Files Git is watching. These could be:
- Unmodified
- Modified
- Staged
3. Staged
Files you have marked ready for the next commit using git add.
4. Committed
Files stored in Git history.
Below is the official Git file state flow:
Untracked → Staged → Committed → Modified → Staged → Committed → …
2. Checking the Current Status
Use this frequently:
git statusIt will show:
- Untracked files
- Modified files
- Staged files
- Ignored files (if you request it)
Untracked Git Files Explained
These are files Git has never seen.
Example:
echo "Notes" > notes.txt
git status
You will see:
Untracked files:
notes.txt
✔ To start tracking:
git add notes.txt✔ To track everything:
git add .4. Tracked Files
Once Git knows about a file, it becomes tracked.
Tracked files can be:
Unmodified
No change since last commit.
Modified
Content changed, but not staged.
Staged
Changes have been added to the staging area using git add.
Example:
echo "More text" >> notes.txt
git statusYou will see:
Changes not staged for commit:
modified: notes.txt
Stage it:
git add notes.txt5. Staged Files
Staged files are prepared for committing.
Example:
git add myfile.txtThis moves it from “modified” → “staged”.
Now commit:
git commit -m "Updated myfile"6. Ignored Files – Using .gitignore
Some files should never be tracked:
- Log files
- Temporary files
- Build output
- Secrets
- Cache folders
- OS-generated files
This is where .gitignore comes in.
7. Creating a .gitignore File
Create a file named:
.gitignoreAdd patterns of files you want Git to ignore.
Example .gitignore:
# OS files
Thumbs.db
.DS_Store
# Logs
*.log
# Dependencies
node_modules/
# Python cache
__pycache__/
# IDE settings
.vscode/
.idea/
# Environment secrets
.env
Apply it:
git statusGit will now hide these from the untracked list.
8. Important: .gitignore Only Works for Untracked Files
If a file is already tracked, ignoring it won’t remove it.
Example:
.env file committed accidentallyThen you add .env inside .gitignore.
Git will still track it.
To stop tracking:
git rm --cached .env9. View Ignored Files
git status --ignored10. Realistic Hands-On Example
mkdir git-files-demo
cd git-files-demo
git init
Create files:
echo "Hello" > app.txt
echo "Secret key" > secrets.env
echo "Debug info" > debug.log
Check status:
git statusAdd .gitignore:
*.log
*.env
Now status shows:
Untracked files:
app.txt
Ignored files:
debug.log
secrets.env
Track only app.txt:
git add app.txt
git commit -m "Added app file"
11. Best Practices for Git File Handling
✔ Always create .gitignore early
Avoid committing secrets or junk files.
✔ Don’t commit:
- .env
- Credentials
- Cache
- Build artifacts
- OS files
✔ Review git status before each commit
Avoid accidental commits.
✔ Use global .gitignore for OS noise
For example:
git config --global core.excludesfile ~/.gitignore_globalAdd:
.DS_Store
Thumbs.db
*.swp12. What’s Next?
Now you understand:
✔ Tracked vs untracked
✔ Modified vs staged
✔ How .gitignore works
✔ How files move in Git
✔ How to avoid accidental commits
Next up:
➡️ Part 5 – Git Staging: Understanding the Staging Area & How It Works
You’ll learn how the staging area acts as Git’s “preview zone,” how partial staging works, and how to manage changes like a pro.
For more information about Git best practices, refer to the official Git documentationGit Get Started – Creating Your First Repository & Making Your First Commit and our comprehensive guide on repository initialization.