Skip to content

Git Cheatsheet

Terminal window
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --list # View all config
git config user.name "Local Name" # Per-repo override (no --global)
Terminal window
git init # Initialize new repo
git clone [url] # Clone existing repo
Terminal window
git add [file] # Stage specific file
git add . # Stage all changes
git commit -m "msg" # Commit staged changes
git status # View current status
git log # View commit history
Terminal window
git branch [name] # Create new branch
git checkout [branch] # Switch to branch
git checkout -b [branch] # Create & switch to branch
git merge [branch] # Merge branch into current
git branch -d [branch] # Delete branch
Terminal window
git remote -v # List remote repos
git remote add origin [url] # Add remote
git push origin [branch] # Push to remote
git pull origin [branch] # Fetch & merge
git fetch origin # Fetch without merging
Terminal window
git stash # Stash current changes
git stash list # View all stashes
git stash pop # Apply & remove latest stash
git stash apply stash@{n} # Apply specific stash
git stash -u # Stash including untracked files
Terminal window
git diff # View unstaged changes
git diff --cached # View staged changes
git diff branch1..branch2 # Compare branches
git log --graph # Visualize commit history
git log -p [file] # Show changes for file
Terminal window
git tag # List tags
git tag -a [name] -m "msg" # Create annotated tag
git push origin --tags # Push all tags
Terminal window
git merge --abort # Cancel merge
git checkout --ours [file] # Keep current branch version
git checkout --theirs [file] # Accept incoming version
Terminal window
git clean -n # Dry run (preview deletions)
git clean -f # Delete untracked files
graph LR
A["Working Directory<br/>(git add)"] -->|git add| B["Staging Area<br/>(git commit)"]
B -->|git commit| C["Local Repo<br/>(git push)"]
C -->|git push| D["Remote Repo"]
D -->|git fetch| E["Fetch without merge"]
D -->|git pull| F["Fetch + Merge"]
E -->|git merge| C
F -->|auto-merge| C
G["Stash Area"] -->|git stash| A
A -->|git stash pop| G

Each strategy fits different team sizes, release cadences, and deployment needs.

StrategyPrimary CharacteristicsAdvantagesUse CasesDrawbacks
Feature BranchBranch per feature, merge to mainSimple, parallel developmentMost small to mid-size teamsMerge conflicts if long-lived
GitHub FlowBranch → PR → review → merge → deployContinuous delivery, simple processSaaS, frequent releasesLess structured for complex releases
Gitflowdevelop + main + feature/release/hotfixClear separation of concerns, planned releasesLarger projects, scheduled releasesMore complex, more branch overhead
Trunk-BasedShort-lived branches, frequent mergesOptimized for CI/CD, fewer conflictsHigh-velocity teams, feature flagsRequires strong testing discipline
ForkingContributors fork, submit PRsDecentralized, open-source standardOpen-source, external contributorsExtra PR workflow step

Next Steps: Master one branching strategy for your team. Practice reset/revert scenarios locally before using on shared branches.