Git Cheat Sheet

Comprehensive git command reference with interactive examples. Search, filter, and copy commands instantly.

🔍
  main     ──●──────────────────●──────────────────●── (stable)
              \                  ↑ merge            ↑ merge
               \                 |                  |
  feature/A     ●───●───●───●──→●                  |
                                                    |
  feature/B          ●───●───●───●───●────────────→●
                     ↑                              |
                  branch from main            squash merge

  ┌─────────────────────────────────────────────────────────┐
  │  1. git checkout -b feature/A main    Create branch     │
  │  2. git add . && git commit -m "..."  Work & commit     │
  │  3. git push -u origin feature/A      Push to remote    │
  │  4. Open Pull Request                 Code review       │
  │  5. git checkout main && git pull     Update main       │
  │  6. git merge feature/A               Merge feature     │
  │  7. git branch -d feature/A           Clean up          │
  └─────────────────────────────────────────────────────────┘

About This Git Cheat Sheet

This interactive Git cheat sheet provides a comprehensive reference for the most commonly used Git commands. Whether you're a beginner learning version control or an experienced developer who needs a quick refresher, this guide covers everything from basic setup and staging to advanced operations like rebasing, stashing, and managing submodules.

Git Workflow Best Practices

A solid Git workflow is essential for team collaboration. The feature branch workflow is the most widely adopted pattern: create a new branch for each feature or bug fix, make commits on that branch, push it to the remote, open a pull request for code review, and merge it back into the main branch when approved. This keeps the main branch stable and makes it easy to review and revert changes.

  • Commit often: Make small, focused commits with clear messages that explain why a change was made.
  • Branch strategically: Use descriptive branch names like feature/user-auth or fix/login-redirect.
  • Pull before push: Always pull the latest changes from the remote before pushing to avoid conflicts.
  • Use .gitignore: Keep build artifacts, dependencies, and secrets out of version control.

Essential Git Concepts

Git tracks changes across three main areas: the working directory (your files on disk), the staging area (changes prepared for the next commit), and the repository (the committed history). Understanding how changes flow between these areas is key to using Git effectively.

Frequently Asked Questions

What is the difference between git merge and git rebase?

Git merge creates a new merge commit that combines two branches, preserving the full history of both branches. Git rebase replays your commits on top of another branch, creating a linear history. Use merge for shared branches to preserve context, and rebase for local feature branches to keep history clean before merging.

How do I undo the last git commit?

Use 'git reset --soft HEAD~1' to undo the last commit but keep changes staged. Use 'git reset --mixed HEAD~1' to undo and unstage. Use 'git reset --hard HEAD~1' to undo and discard all changes permanently. If already pushed, use 'git revert HEAD' instead.

What is git stash and when should I use it?

Git stash temporarily saves uncommitted changes so you can work on something else, then re-apply them later. Use 'git stash' to save, 'git stash pop' to re-apply and remove, or 'git stash apply' to re-apply while keeping the stash. Useful when switching branches mid-work.

How do I resolve a git merge conflict?

When a conflict occurs, Git marks conflicting sections with <<<<<<<, =======, and >>>>>>> markers. Open each file, decide which changes to keep, remove the markers, stage with 'git add', and complete with 'git commit'. Use 'git status' to see conflicted files.

What is the difference between git pull and git fetch?

Git fetch downloads data from a remote but doesn't integrate it — it updates remote-tracking branches. Git pull is 'git fetch' followed by 'git merge', downloading and immediately integrating changes. Use fetch to review before merging, pull to update directly.

Related Tools

  • Regex Tester — Test and debug regular expressions online with real-time highlighting.
  • JWT Decoder — Decode and debug JSON Web Tokens instantly in your browser.