๐ Version Control
Git & GitHub Complete Cheatsheet
From init to advanced rebasing โ master version control for any project.
01
Setup & Config
โผ
First-time Setup
BASHGit initial configuration
git config --global user.name "Your Name" git config --global user.email "you@email.com" git config --global core.editor "code --wait" # VS Code as editor git config --list # view all settings
Init new repo
git init โ creates .git folder in current directoryClone existing
git clone https://github.com/user/repo.git.gitignore
List files/folders to exclude:
node_modules/ *.env .DS_Store
02
Core Concepts
โผ
The Three Zones
Working Directory
Where you edit files. Changes are 'unstaged'.
Staging Area (Index)
Files added with
git add. Snapshot ready to commit.Repository (.git)
Committed history. Permanent snapshots of your project.
HEAD
Pointer to the current commit/branch you're on.
BASHCheck status
git status # see what's changed git diff # unstaged changes git diff --staged # staged vs last commit git log --oneline # compact commit history git log --graph # visual branch history
03
Daily Workflow
โผ
Daily Commands
BASHThe everyday workflow
# Stage files git add . # stage everything git add src/file.js # stage specific file git add -p # stage by patch (interactive) # Commit git commit -m "feat: add login form" git commit -am "fix: typo" # stage+commit tracked files # Push git push origin main git push -u origin feature/login # -u sets upstream # Pull git pull # fetch + merge git pull --rebase # fetch + rebase (cleaner history) # Fetch (without merging) git fetch origin
๐ก
Write commits in imperative mood: 'Add feature' not 'Added feature'. Follow Conventional Commits: feat:, fix:, docs:, chore:
04
Branching
โผ
Branch Commands
BASHBranching operations
git branch # list local branches git branch -a # list all (including remote) git branch feature/login # create branch git checkout feature/login # switch to branch git checkout -b feature/login # create + switch (shortcut) git switch feature/login # modern syntax git switch -c feature/login # create + switch (modern) # Delete git branch -d feature/login # safe delete (merged) git branch -D feature/login # force delete git push origin --delete feature/login # delete remote
Naming convention
feature/ fix/ hotfix/ release/ chore/Protected branches
main/master should be protected โ always work on feature branches
05
Merging & Rebasing
โผ
Merging vs Rebasing
BASHMerge and rebase
# Merge (preserves history) git checkout main git merge feature/login git merge --no-ff feature/login # always creates merge commit # Rebase (linear history) git checkout feature/login git rebase main # replay feature commits on top of main git rebase -i HEAD~3 # interactive rebase last 3 commits # Squash before merging (clean up messy commits) git rebase -i HEAD~5 # pick, squash, squash...
โน๏ธ
Use merge for public branches (preserves context). Use rebase on private feature branches to keep history clean. NEVER rebase shared branches.
06
Remote / GitHub
โผ
Remote Operations
BASHGitHub / remote workflows
git remote -v # list remotes git remote add origin https://... # add remote # Pull Request workflow git checkout -b feature/new-thing # ... make changes ... git push -u origin feature/new-thing # Open PR on GitHub โ Review โ Merge # Sync fork with upstream git remote add upstream https://original-repo.git git fetch upstream git merge upstream/main
Fork
Your personal copy of someone else's repo on GitHub
PR / MR
Pull Request (GitHub) / Merge Request (GitLab) โ propose changes
GitHub Actions
CI/CD automation triggered by push, PR, schedule etc.
07
Undoing Changes
โผ
Undoing Changes
BASHFix mistakes
# Undo unstaged changes git checkout -- file.js # restore file to last commit git restore file.js # modern syntax # Unstage (keep changes) git reset HEAD file.js git restore --staged file.js # Undo last commit (keep changes staged) git reset --soft HEAD~1 # Undo last commit (keep changes unstaged) git reset --mixed HEAD~1 # Undo last commit (DELETE changes!) git reset --hard HEAD~1 # Revert (safe โ creates new "undo" commit) git revert HEAD git revert abc1234 # revert specific commit # Fix last commit message git commit --amend -m "correct message"
โ ๏ธ
Never use
git reset --hard on shared branches โ it rewrites history and breaks other people's work.
08
Git Stash
โผ
Stash
BASHTemporarily save work
git stash # stash uncommitted changes
git stash push -m "WIP: auth feature"
git stash list # see all stashes
git stash pop # apply latest + remove
git stash apply stash@{1} # apply specific + keep
git stash drop stash@{0} # delete specific stash
git stash clear # remove all stashes
git stash branch new-branch # create branch from stash
09
Advanced Commands
โผ
Power Commands
BASHAdvanced git
# Search through history git log --all --grep="login" # search commit messages git log -S "functionName" # search code changes # See who changed what git blame file.js git log -p file.js # file change history # Cherry-pick specific commit git cherry-pick abc1234 # Find commit that introduced a bug git bisect start git bisect bad HEAD git bisect good v1.0 # git will checkout commits for you to test # Cleanup git gc # garbage collect git remote prune origin # remove stale remote refs
10
Mini Quizzes
โผ
โ Quiz 1
What does
git rebase -i HEAD~3 do?Interactive rebase lets you pick, squash, reword, or drop commits. HEAD~3 means 'last 3 commits'. Perfect for cleaning up before a PR.
โ Quiz 2
Which command safely undoes a commit without deleting code?
git revert creates a NEW commit that undoes the changes. It's safe for shared branches. git reset --hard DELETES changes permanently.
โ Quiz 3
What's the difference between git fetch and git pull?
git fetch downloads remote changes but doesn't touch your local branch. git pull = git fetch + git merge. Use fetch when you want to inspect before merging.