Git Fundamentals
init, add, commit, status, log
Why it matters
These five commands cover 80% of daily Git usage. Master them and you’ll handle most version control tasks with confidence.
Key concepts
- git init — Create a new Git repository in current directory
- git add — Stage files for the next commit
- git commit — Save a snapshot of staged changes
- git status — Show working tree status
- git log — View commit history
The idea
The Checkpoint Analogy
Think of Git like a video game’s save system:
- git init = Start a new game (enable saving)
- git add = Select which items to save (stage)
- git commit = Create a save point (checkpoint)
- git status = Check what’s changed since last save
- git log = View all your save points
Unlike video games, Git keeps every save point forever. You can always go back.
The Workflow
1. Edit files (work in your editor)
↓
2. git status (see what changed)
↓
3. git add <files> (stage changes)
↓
4. git commit -m "message" (create checkpoint)
↓
5. Repeat
Walkthrough
Setup (One-Time)
# Install Git
sudo apt install git # Ubuntu/Debian
brew install git # macOS
# Configure identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Optional: set default branch name
git config --global init.defaultBranch main
Starting a Repository
.git directory.# Create new repository
git init
# Or clone an existing one
git clone https://github.com/user/repo.git
The Daily Workflow
# Check current status
git status
# Stage specific files
git add file.txt
git add src/
# Stage all changes
git add .
# Commit with message
git commit -m "Add login feature"
# View commit history
git log
git log --oneline # Compact view
git log --graph # Show branch structure
Understanding Status Output
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: staged_file.txt ← Ready to commit
Changes not staged for commit:
modified: unstaged_file.txt ← Changed but not staged
Untracked files:
new_file.txt ← Git doesn't know about this
Key takeaways
- git init creates the .git folder (repository)
- git add stages changes (prepares for commit)
- git commit creates a permanent snapshot
- git status shows what’s staged/unstaged/untracked
- git log shows commit history
Dos & don’ts
✅ DO
- Commit often: Small, focused commits are easier to understand and revert
- Write meaningful messages: “Fix null pointer in auth” not “fix bug”
- Check status before committing: Make sure you’re committing what you expect
❌ DON’T
- Don’t commit everything at once: Stage related changes together
- Don’t use
git add .blindly: Review what you’re staging - Don’t leave work uncommitted overnight: Commit your progress
Going deeper
The .git folder: Everything Git knows lives in .git/. The objects/ folder stores all file content (as compressed blobs). The refs/ folder stores branch pointers. HEAD is a file pointing to your current branch.
Staging area internals: The staging area (index) is a binary file at .git/index. It’s a snapshot of what your next commit will look like. This two-step process (stage then commit) gives you control over what goes into each commit.
Common mistakes
Committing without staging:
git commit -m "message" only commits staged changes. If you didn’t git add, nothing is committed.
Use git commit -am "message" to add+commit tracked files (but not new files).
Forgetting to init:
“fatal: not a git repository” means you’re not in a Git repo. Run git init or navigate to the right folder.