⬅️ Home • Phase 1: Beginners • Phase 2: Intermediate ➡️ • ⚡ Cheat Sheet
Welcome to Phase 1! Git can feel intimidating at first with its jargon and terminal commands, but beneath the surface, it works on a simple principle:
Git is a time machine and save-state system for your code.
Every commit is a permanent snapshot of your project at a specific moment in time. If you break something, you can always travel back.
To master Git, you need to understand where your files live at any given moment. Git moves code across 4 key zones:
flowchart LR
subgraph Local Machine
A["📁 1. Working Directory<br/><i>(Your actual files on disk)</i>"]
B["📦 2. Staging Area (Index)<br/><i>(Prepped changes for commit)</i>"]
C["💻 3. Local Repository<br/><i>(.git history & snapshots)</i>"]
end
subgraph Cloud
D["☁️ 4. Remote Repository<br/><i>(GitHub / GitLab)</i>"]
end
A -- "git add" --> B
B -- "git commit" --> C
C -- "git push" --> D
D -- "git pull" --> A
D -- "git fetch" --> C
- Untracked: Git sees the file in your folder, but it is not part of history yet.
- Modified: You changed an existing tracked file in your working directory.
- Staged: You marked the file changes with
git addto be included in the next snapshot. - Committed: The staged snapshot is permanently recorded into the
.gitdatabase.
Before creating commits, configure your global identity so your teammates and GitHub know who authored each change:
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name to 'main'
git config --global init.defaultBranch main
# Verify your configuration
git config --listYou can start a Git-tracked project in one of two ways:
mkdir my-awesome-app
cd my-awesome-app
git initNote
git init creates a hidden .git folder in your directory. This folder is the "brain" containing all history, objects, and configuration. Never delete .git unless you want to erase all version history!
git clone https://github.com/username/project-name.git
cd project-nameYou will execute this 4-step loop dozen of times every day:
[Edit Files] ➔ git status ➔ git diff ➔ git add ➔ git commit
Always run git status before doing anything. It is your GPS in Git:
git statusOutput tells you: Which branch you are on, what files are staged (green), and what files are modified/untracked (red).
Before you stage files, inspect exactly what lines you added, changed, or deleted:
# View unstaged changes in working directory vs last commit
git diff
# View staged changes (what is about to be committed)
git diff --stagedThe Staging Area lets you curate which changes belong in the next commit:
# Stage a specific file
git add index.html
# Stage multiple specific files
git add styles.css app.js
# Stage ALL modified and new files in the project
git add .Wrap up staged changes into a permanent snapshot with a descriptive message:
git commit -m "feat: implement user registration form"Tip
Clear commit messages make debugging and teamwork seamless. Use the standard prefixes:
feat:A brand new feature (feat: add dark mode toggle)fix:A bug fix (fix: resolve mobile navbar overflow)docs:Documentation changes only (docs: update setup steps in README)style:Formatting, missing semicolons, no code logic changes (style: format with prettier)refactor:Code restructuring without changing behavior (refactor: simplify auth middleware)chore:Updating build tools, dependencies, configs (chore: bump vite to v6.0)
# View standard full commit log
git log
# View clean, compact single-line history
git log --oneline
# View history with branch graphs
git log --oneline --graph --decorate -n 10Not every file belongs in version control! You should never commit:
- API keys, secrets,
.envfiles. - Heavy build artifacts (
dist/,build/,target/). - Package dependencies (
node_modules/,venv/,vendor/). - Operating system cache files (
.DS_Store,Thumbs.db).
Create a file named .gitignore in your project root:
# Dependencies
node_modules/
__pycache__/
venv/
# Environment Variables & Secrets
.env
.env.local
*.pem
id_rsa
# Build outputs
dist/
build/
*.log
# OS temporary files
.DS_Store
Thumbs.dbWarning
What if you already tracked a file you now want to ignore?
Simply adding it to .gitignore won't remove it from Git's tracking. You must untrack it first:
git rm --cached .env
git commit -m "chore: stop tracking .env secret"Made a mistake? Don't panic. Here are the most common early fixes:
git restore --staged filename.js(Your file edits remain completely intact in your working directory; it is simply unstaged).
git restore filename.js(
# Stage the forgotten file
git add forgotten-file.js
# Amend the last commit without creating a duplicate commit
git commit --amend -m "feat: complete user registration form"Once your local repository has commits, connect it to GitHub to share and back it up.
Go to GitHub.com/new and create a new, empty repository (do not initialize with README if you already have local files).
# Link local repo to GitHub (SSH syntax recommended)
git remote add origin git@github.com:your-username/my-awesome-app.git
# Verify remote configuration
git remote -v
# Rename current branch to 'main'
git branch -M main
# Push and set upstream tracking
git push -u origin mainNote
The -u (or --set-upstream) flag links your local main branch to origin/main. In future sessions, you only need to type:
git push # to upload new commits
git pull # to download updates from GitHubBefore moving to Phase 2, make sure you can:
- Explain the 4 Git zones (Working Dir, Staging, Local Repo, Remote).
- Use
git status,git diff,git add, andgit commitcomfortably. - Create a
.gitignorefile to ignore secrets andnode_modules. - Push local commits to a GitHub remote with
git push -u origin main.