đ Git Cheat Sheet
Essential git commands for macOS. Pro-level shortcuts and workflows.
âī¸ Setup & Config
Set global username
Configure your identity for all repos
git config --global user.name "Your Name"
Set global email
git config --global user.email "you@email.com"
Set default branch name
Use 'main' instead of 'master'
git config --global init.defaultBranch main
View all config
git config --global --list
Set VS Code as default editor
git config --global core.editor "code --wait"
Enable colored output
git config --global color.ui auto
đĻ Basics
Initialize a repo
git init
Clone a repo
git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git # SSH
Check status
git status git status -s # short format
Stage files
git add file.txt # single file git add . # all changes git add -p # interactive patch staging
Commit
git commit -m "feat: add login screen" git commit -am "fix: update model" # stage + commit tracked files git commit --amend --no-edit # amend last commit (keep message)
Unstage a file
git restore --staged file.txt
Discard working tree changes
git restore file.txt # discard file changes git restore . # discard all changes
Show diff
git diff # unstaged changes git diff --staged # staged changes git diff main..feature # between branches
đŋ Branching
List branches
git branch # local git branch -r # remote git branch -a # all
Create & switch branch
git switch -c feature/login # recommended git checkout -b feature/login # older style
Switch branch
git switch main git checkout main # older style
Merge branch into current
git merge feature/login git merge --no-ff feature/login # always create merge commit git merge --squash feature/login # squash all commits into one
Delete branch
git branch -d feature/login # safe delete git branch -D feature/login # force delete
Rename branch
git branch -m old-name new-name
đ Remote
Show remotes
git remote -v
Add remote
git remote add origin https://github.com/user/repo.git
Push
git push origin main git push -u origin feature/login # set upstream git push --force-with-lease # safer force push
Pull
git pull git pull --rebase # pull with rebase instead of merge git pull origin main # explicit
Fetch
git fetch origin git fetch --prune # fetch + remove deleted remote branches
Delete remote branch
git push origin --delete feature/login
Track remote branch
git branch --set-upstream-to=origin/main main
đ History & Log
View log
git log git log --oneline git log --oneline --graph --all # pretty branch graph
Search commits
git log --grep="login" # by message git log -S "functionName" # by code change git log --author="Feem" # by author
Show commit details
git show abc1234 git show HEAD~1 # one before latest
Who changed this line?
git blame file.txt git blame -L 10,20 file.txt # lines 10â20
Pretty log alias
git log --oneline --graph --decorate --all
đĻ Stash
Stash changes
git stash git stash push -m "WIP: login screen" # with message git stash -u # include untracked
List stashes
git stash list
Apply stash
git stash pop # apply and remove top stash
git stash apply stash@{1} # apply specific stash (keep it)Drop stash
git stash drop stash@{0}
git stash clear # remove all stashesâŠī¸ Reset & Undo
Undo last commit (keep changes staged)
git reset --soft HEAD~1
Undo last commit (keep changes unstaged)
git reset HEAD~1 # --mixed is default
Undo last commit (discard all changes)
â ī¸ Destructive â cannot undo
git reset --hard HEAD~1
Revert a commit (safe â creates new commit)
git revert abc1234 git revert HEAD # revert latest commit
Cherry pick a commit
git cherry-pick abc1234 git cherry-pick abc1234 def5678 # multiple
đ Rebase
Rebase onto main
git rebase main
Interactive rebase (squash, edit commits)
git rebase -i HEAD~3 # last 3 commits # s = squash, r = reword, d = drop, e = edit
Continue / abort rebase
git rebase --continue git rebase --abort
đˇī¸ Tags
Create tag
git tag v1.0.0 git tag -a v1.0.0 -m "Release 1.0.0" # annotated
List & push tags
git tag git push origin v1.0.0 git push origin --tags # push all tags
Delete tag
git tag -d v1.0.0 git push origin --delete v1.0.0 # remote
⥠Useful Aliases
Add useful global aliases
git config --global alias.st "status -s" git config --global alias.co "checkout" git config --global alias.br "branch" git config --global alias.lg "log --oneline --graph --all --decorate" git config --global alias.undo "reset HEAD~1" git config --global alias.save "!git add -A && git commit -m 'WIP'"
Use alias
git lg git st git undo
đ Common Workflows
Feature branch workflow
# 1. Create feature branch from main git switch main && git pull git switch -c feature/my-feature # 2. Work, commit git add . && git commit -m "feat: ..." # 3. Push for review git push -u origin feature/my-feature # 4. After merge, clean up git switch main git pull git branch -d feature/my-feature
Conventional commit messages
feat: new feature fix: bug fix refactor: code change (no feat/fix) docs: documentation style: formatting test: adding tests chore: build process, tooling
Clean up merged branches
# Delete local branches already merged to main git branch --merged main | grep -v "main" | xargs git branch -d # Prune stale remote tracking branches git remote prune origin