Developer Reference
Git & GitHub
Cheat Sheet
A concise, hands-on reference for every Git command — with real examples, GitHub workflow, and recovery techniques all in one place.
$ every command · with examples · explained simply
// What is Git?
🗂️
Version Control SystemTracks every change to your code — what, when, and who. Roll back anytime.
🌿
Branching & MergingWork on features in isolation. Merge when ready, discard if not.
👥
CollaborationMultiple people, one codebase. Git keeps changes in sync without conflicts.
☁️
GitHub = Cloud + CollaborationHosts your repos online and adds Pull Requests, Issues, and CI/CD.
01 — Setup & Initialize
⚙️
Global Config
Set your identity once
Set username & email
git config --global user.name "John Doe" git config --global user.email "john@email.com"
Set default editor & branch name
git config --global core.editor "code" git config --global init.defaultBranch main
View all config
git config --list🚀
Init & Clone
Start a repo locally or from remote
Create new local repo
git init my-project cd my-project
Clone from GitHub
git clone https://github.com/user/repo.gitClone into a specific folder
git clone https://github.com/user/repo.git my-folder02 — Staging & Commits
📦
Staging Files
Choose what goes into the next commit
Check current status
git statusStage a specific file
git add index.htmlStage all changed files
git add .Unstage a file
git restore --staged index.htmlSee what's staged vs unstaged
git diff --staged✅
Committing
Save a snapshot of staged changes
Commit with a message
git commit -m "feat: add login page"Stage + commit tracked files at once
git commit -am "fix: typo in header"Amend last commit message
git commit --amend -m "fix: correct message"Empty commit (trigger CI)
git commit --allow-empty -m "chore: trigger CI"03 — Branching
🌿
Branch Basics
Create, switch, list, delete
List all branches
git branch # local git branch -a # local + remote
Create a new branch
git branch feature/loginSwitch to a branch
git switch feature/loginCreate & switch in one step
git switch -c feature/signupDelete a branch
git branch -d feature/login # safe git branch -D feature/login # force
Rename current branch
git branch -m new-name🔀
Merge & Rebase
Combine branch histories
Merge a branch into current
git switch main git merge feature/login
Merge without fast-forward (keeps history)
git merge --no-ff feature/loginRebase onto main (cleaner history)
git switch feature/login git rebase main
Abort a conflicting rebase
git rebase --abortContinue after resolving conflicts
git rebase --continue04 — Remote & GitHub
☁️
Remote Commands
Connect & sync with GitHub
Add a remote (GitHub)
git remote add origin https://github.com/user/repo.gitView remotes
git remote -vPush branch to GitHub
git push origin mainPush & set upstream (first time)
git push -u origin feature/loginPull latest from remote
git pull origin mainFetch (download without merge)
git fetch originDelete a remote branch
git push origin --delete feature/login🐙
GitHub Workflow
Fork → PR → Review → Merge
1
Fork & clone repo from GitHub
git clone https://github.com/you/forked-repo.git2
Create a feature branch
git switch -c feature/my-change3
Commit your changes
git add . git commit -m "feat: my change"
4
Push to your fork
git push -u origin feature/my-change5
Open Pull Request on GitHub UI
# Go to GitHub → Compare & Pull Request # Add title, description → Submit PR
6
Sync with upstream after merge
git switch main git pull upstream main
05 — Undo & Recovery
↩️
Undo Changes
Revert, reset, restore
Discard changes in working directory
git restore index.htmlUndo last commit (keep changes)
git reset --soft HEAD~1Undo last commit (discard changes) ⚠️
git reset --hard HEAD~1Revert a commit (safe, creates new commit)
git revert a3f1b2cRecover lost commits with reflog
git reflog git reset --hard HEAD@{3}
🗄️
Stash
Save work temporarily without committing
Stash current changes
git stashStash with a label
git stash push -m "WIP: login form"List all stashes
git stash listApply latest stash
git stash popApply a specific stash
git stash apply stash@{2}Drop a stash
git stash drop stash@{0}06 — Log & History
📜
git log
Inspect commit history
Basic log
git logCompact one-line log
git log --onelineVisual branch graph
git log --oneline --graph --allFilter by author
git log --author="Alice"Log since a date
git log --since="2026-01-01"Reflog (every HEAD movement)
git reflog07 — Advanced Commands
🍒
Cherry-pick
Copy specific commits to current branch
Apply a single commit from another branch
git cherry-pick a3f1b2cCherry-pick a range of commits
git cherry-pick a3f1b2c..f9e3d11Cherry-pick without committing
git cherry-pick -n a3f1b2c🔍
Diff & Blame
Inspect what changed and who changed it
See unstaged changes
git diffCompare two branches
git diff main..feature/loginSee who changed each line
git blame index.htmlSearch for a string in history
git log -S "loginUser" --oneline✂️
Interactive Rebase
Rewrite, squash, edit commits
Squash last 3 commits interactively
git rebase -i HEAD~3In the editor that opens:
pick a3f1b2c feat: add login squash 9d4e521 fix: typo squash 1bc7830 fix: spacing # → becomes 1 clean commit
📁
.gitignore
Tell Git what to never track
Example .gitignore file
# Dependencies node_modules/ # Environment secrets .env .env.local # Build output dist/ build/ # OS files .DS_Store Thumbs.db
Ignore already-tracked file
git rm --cached .env08 — Quick Reference
| Command | What it does |
|---|---|
git status | Show working tree status |
git add . | Stage all changes |
git commit -m "msg" | Commit with message |
git push origin main | Push to GitHub |
git pull origin main | Pull latest from GitHub |
git switch -c branch | Create + switch to branch |
git merge branch | Merge branch into current |
git stash | Save changes temporarily |
git stash pop | Restore stashed changes |
git log --oneline | Compact commit history |
git reflog | Every HEAD movement |
git reset --soft HEAD~1 | Undo last commit, keep changes |
git reset --hard HEAD~1 | Undo last commit, discard changes |
git revert abc123 | Safe undo via new commit |
git cherry-pick abc123 | Copy commit to current branch |
git tag v1.0.0 | Create a version tag |
git diff main..branch | Compare two branches |
git blame file.txt | See who wrote each line |
git rebase -i HEAD~3 | Squash/edit last 3 commits |
git remote -v | List remotes with URLs |