Git & Command Line•22 min read•Beginner
Git — Your First Repo
init, add, commit, status, log. The 5 commands you'll run a hundred times a day.
What Git is
Git is a VERSION CONTROL system: a time machine for your code. Every project bigger than a single file should be in Git. It tracks every change, lets you roll back mistakes, branch off to try ideas safely, and merge work from multiple people.
💾
Real-life analogy — Git = save points in a video game
Every commit is a save point. You can return to any save point later. Branches are alternate timelines — try a risky boss fight, and if you die, jump back to the save and take a different path.
First-time setup
bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainStarting a repo
bash
mkdir my-project
cd my-project
git init # turn a folder into a git repo
echo '# My Project' > README.md
git status # see what's untracked or modified
git add README.md # stage a file
git commit -m "first commit" # snapshot itThe three areas
- Working directory — files you're editing right now.
- Staging area (index) — changes you've added but not yet committed. Lets you commit only some changes.
- Repository — the timeline of committed snapshots.
Daily commands
bash
git status # what's changed?
git add . # stage every change
git add file.py # stage just one file
git add -p # interactively pick which CHUNKS to stage
git commit -m "fix bug X" # snapshot the staged changes
git log # history
git log --oneline # one line per commit
git diff # unstaged changes
git diff --staged # staged-but-not-committed changes.gitignore
Some files should never be committed — secrets, build artifacts, dependencies. List them in a `.gitignore` file at the repo root.
bash
# .gitignore
node_modules/
.env
*.log
.DS_Store
.next/
__pycache__/⚠ Watch out
If you accidentally commit a secret (API key, password), rotating that secret is the only safe fix. Removing it from git history is hard and incomplete — assume the secret is leaked the moment it touches a commit.