Git is a source control system built to answer a simple question: what changed, when did it change, and why? In this first post, you will build a practical foundation you can reuse in every repository.
Table of Contents
This post builds a practical Git foundation you can reuse in every repository.
Core Concepts
Setup
Commit Workflow
- Make your first commit with intent
- Review changes before committing
- Keep noise out of history with .gitignore
Recovery
Series Navigation
What Git is actually tracking
Git tracks snapshots of your project over time. A commit is not a “diff file”; it is a recorded state of tracked files plus metadata (author, date, message, and parent commit references).
That design gives you three immediate advantages:
- You can review exactly what changed before saving a commit
- You can move backward and forward through project history
- You can create isolated branches without copying entire directories by hand
Configure identity once
Before you commit in any repository, set your name and email. This data becomes part of commit history.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainCheck your configuration:
git config --list --show-originStart from scratch or clone an existing repository
Create a new repository:
mkdir demo-git-basics
cd demo-git-basics
git initOr clone one that already exists:
git clone https://example.com/org/project.git
cd projectUnderstand the three states: working tree, index, and HEAD
Most confusion disappears when you know these three locations:
- Working tree: files on disk that you are actively editing
- Index (staging area): the exact set of changes queued for the next commit
- HEAD: the currently checked-out commit
Use this cycle constantly:
git status
git add <file>
git status
git commit -m "Describe why this change exists"git status is your ground truth. If you feel lost, run it first.
Make your first commit with intent
Try a small commit:
echo "# Demo Project" > README.md
git status
git add README.md
git commit -m "Create project README with initial heading"Now inspect history:
git log --oneline --decorate --graphThis one-line format is a great daily view, especially as branches are introduced later in this series.
Review changes before committing
Git lets you inspect changes in detail at each stage:
# unstaged changes (working tree vs index)
git diff
# staged changes (index vs HEAD)
git diff --stagedFor cleaner commits, stage only parts of a file:
git add -pThis helps separate unrelated edits into focused commits that are easier to review and revert.
Keep noise out of history with .gitignore
Use .gitignore to prevent build artifacts, secrets, and local environment files from being committed.
# .gitignore
.env
*.log
dist/
node_modules/If a file is already tracked, adding it to .gitignore does not remove it from history automatically. You must untrack it first.
git rm --cached .env
git commit -m "Stop tracking local environment file"Safe local undo before pushing
Two common local recovery actions:
# restore a file in working tree back to last committed state
git restore path/to/file
# unstage a file but keep your edits
git restore --staged path/to/fileUse these to clean up local mistakes before your changes are shared.
Practice checklist
Run this sequence in a practice repository until it feels automatic:
- Edit one file.
- Run
git status. - Run
git diff. - Stage with
git add -p. - Run
git diff --staged. - Commit with a message that explains why.
- Inspect with
git log --oneline.
Once this loop is natural, the next topic becomes much easier.
Next in this series
In the next post, we move from single-line history to parallel work with branches, merge strategies, and conflict resolution