Step 2
Step 2 — Your first Git commit
0 views
Step 2 — Your first Git commit
"Roll back to last month", "collaborate with a friend", and "keep my code safe" — one tool solves all three.
1. What Git stores
Snapshots of the folder at each point in time. One commit = one snapshot.
t0: README.md "hi"
t1: README.md "hi world" ← commit 1
t2: README.md "hi world!" ← commit 2
Each commit references the previous by hash, forming a chain.
2. Install
# Windows
winget install Git.Git
# macOS
brew install git
# Linux
sudo apt install git
git --version # 2.40+
3. One-time config
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf input # (Windows) line endings
Use a real email — commits are tagged with it.
4. First repo
mkdir my-first-repo
cd my-first-repo
git init
A hidden .git/ folder now tracks history.
5. Three areas
Working tree → Staging → Repository
editing add commit
- Edit — Git only notices changes
- add — stage for next commit
- commit — snapshot sealed
echo "# My project" > README.md
git status
git add README.md
git status
git commit -m "docs: first README"
git log
6. Ten basic commands
git status
git diff
git diff --staged
git add .
git commit -m "msg"
git log --oneline
git log --graph --oneline
git branch
git checkout -b feature-x
git merge feature-x
7. Branches — parallel worlds
git checkout -b fix-login
# edit
git add . && git commit -m "fix: login"
git checkout main
git merge fix-login
A branch = another line of snapshots diverging from a point on main.
8. Connect to GitHub
Create an empty repo on GitHub, then:
git remote add origin https://github.com/you/repo.git
git push -u origin main
Subsequent pushes just use git push.
9. Collaboration cycle
git pull
git checkout -b fix-x
# work
git add . && git commit -m "fix: x"
git push -u origin fix-x
# Open a Pull Request on GitHub
10. Commit messages
Subject line + blank + body.
feat: add signup page
- email / password inputs
- POST to /api/signup
- handle loading and errors
Common tags: feat, fix, docs, refactor, test, chore.
11. Undoing
git commit --amend -m "new msg"
git restore --staged file
git restore file # discard edits (careful)
git reset HEAD~1 # undo last commit, keep edits
Avoid reset --hard as a beginner.
12. .gitignore
node_modules/
.next/
.env
.env.local
*.log
dist/
.DS_Store
Never commit .env — rotate secrets if you do.
13. Gotchas
- Accidentally committing
.env— rotate secrets, history is hard to scrub - HTTPS push requires a PAT (or use SSH keys)
- Merge conflicts — resolve manually, then
git add+commit - Lost state —
git reflogshows every HEAD move; usually recoverable
Closing
Ten commands cover 80% of real work. Learn the rest as you need it. Make git status a reflex.
Next
- 03-vscode
References: Pro Git Book (free) · GitHub Skills · Oh Shit, Git!?!.