Git Cheatsheet
Git Setup & Configuration
Section titled “Git Setup & Configuration”git config --global user.name "Your Name"git config --global user.email "your@email.com"git config --list # View all configgit config user.name "Local Name" # Per-repo override (no --global)Repository Initialization
Section titled “Repository Initialization”git init # Initialize new repogit clone [url] # Clone existing repoStaging & Committing
Section titled “Staging & Committing”git add [file] # Stage specific filegit add . # Stage all changesgit commit -m "msg" # Commit staged changesgit status # View current statusgit log # View commit historyBranching
Section titled “Branching”git branch [name] # Create new branchgit checkout [branch] # Switch to branchgit checkout -b [branch] # Create & switch to branchgit merge [branch] # Merge branch into currentgit branch -d [branch] # Delete branchRemote Operations
Section titled “Remote Operations”git remote -v # List remote reposgit remote add origin [url] # Add remotegit push origin [branch] # Push to remotegit pull origin [branch] # Fetch & mergegit fetch origin # Fetch without mergingStashing (Temporary Storage)
Section titled “Stashing (Temporary Storage)”git stash # Stash current changesgit stash list # View all stashesgit stash pop # Apply & remove latest stashgit stash apply stash@{n} # Apply specific stashgit stash -u # Stash including untracked filesDiff & Inspection
Section titled “Diff & Inspection”git diff # View unstaged changesgit diff --cached # View staged changesgit diff branch1..branch2 # Compare branchesgit log --graph # Visualize commit historygit log -p [file] # Show changes for fileTagging
Section titled “Tagging”git tag # List tagsgit tag -a [name] -m "msg" # Create annotated taggit push origin --tags # Push all tagsMerge Conflicts
Section titled “Merge Conflicts”git merge --abort # Cancel mergegit checkout --ours [file] # Keep current branch versiongit checkout --theirs [file] # Accept incoming versionCleanup
Section titled “Cleanup”git clean -n # Dry run (preview deletions)git clean -f # Delete untracked filesVisual Git Workflow
Section titled “Visual Git Workflow”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| GBranching Strategies
Section titled “Branching Strategies”Each strategy fits different team sizes, release cadences, and deployment needs.
| Strategy | Primary Characteristics | Advantages | Use Cases | Drawbacks |
|---|---|---|---|---|
| Feature Branch | Branch per feature, merge to main | Simple, parallel development | Most small to mid-size teams | Merge conflicts if long-lived |
| GitHub Flow | Branch → PR → review → merge → deploy | Continuous delivery, simple process | SaaS, frequent releases | Less structured for complex releases |
| Gitflow | develop + main + feature/release/hotfix | Clear separation of concerns, planned releases | Larger projects, scheduled releases | More complex, more branch overhead |
| Trunk-Based | Short-lived branches, frequent merges | Optimized for CI/CD, fewer conflicts | High-velocity teams, feature flags | Requires strong testing discipline |
| Forking | Contributors fork, submit PRs | Decentralized, open-source standard | Open-source, external contributors | Extra PR workflow step |
Next Steps: Master one branching strategy for your team. Practice reset/revert scenarios locally before using on shared branches.