Git Commands Cheat Sheet: Complete Guide for Developers

You’re in the middle of a critical bug fix. You need to create a new branch, but you can’t remember: is it git checkout-b or git branch-b? You Google it for the hundredth time this month.

Sound familiar? Git has 160+ commands, and even experienced developers constantly look up the syntax.

This Git commands cheat sheet solves that problem. It’s the most comprehensive, modern reference for Git commands you actually use, organized by what you’re trying to accomplish, not alphabetical order.

What makes this different:

  • Modern syntax (Git 2.23+ commands like git switch and git restore)
  • 50+ code examples (copy-paste ready)
  • Workflow-focused (organized by task, not alphabetically)
  • Troubleshooting (common mistakes and how to fix them)

Quick Navigation


Branch Management

Creating and Switching Branches

Create a new branch and switch to it:

git checkout -b feature-name

This is the most common branching command. It combines two operations:

  1. Creates the branch: git branch feature-name
  2. Switches to it: git checkout feature-name

💡 Modern (Git 2.23+): git switch -c feature-name
The switch command is clearer, it only handles branches, while checkout does multiple things.

Create a branch from another branch:

git checkout -b new-branch existing-branch

# Example: Create feature branch from develop
git checkout -b feature/user-auth develop

Switching Between Branches

Switch to an existing branch:

git checkout branch-name
# Modern: git switch branch-name

Switch to the previous branch:

git checkout -

This is like the “back” button in your browser, super useful when switching between two branches frequently.

Checking Current Branch

# Full status
git status

# Just branch name
git branch --show-current

Staging & Committing

Checking Status

git status        # Full status
git status -s     # Short format

Staging Files

# Stage a specific file
git add filename.js

# Stage all changes
git add .

# Stage all JS files
git add *.js

# Stage parts of a file (interactive)
git add -p filename.js

Unstaging Files

# Modern way (Git 2.23+)
git restore --staged filename.js

# Old way (still works)
git reset HEAD filename.js

Discarding Changes

# Discard changes in a file (modern)
git restore filename.js

# Discard ALL changes
git reset --hard HEAD

⚠️ Warning: git reset --hard permanently deletes changes!


Undoing Changes

Removing Commits

# Delete last commit (keep changes)
git reset --soft HEAD~1

# Delete last commit (discard changes)
git reset --hard HEAD~1

# Delete last 3 commits
git reset --hard HEAD~3

Resetting to Remote

# Reset to match remote
git fetch origin
git reset --hard origin/main

# Shorthand
git reset --hard @{u}

Undoing Merges

# Undo merge (if not committed)
git merge --abort

# Undo merge (if already committed)
git reset --hard HEAD~1

Remote Operations

Cloning

# Clone repository
git clone https://github.com/user/repo.git

# Clone specific branch
git clone -b branch-name https://github.com/user/repo.git

#Shallow clone (faster for large repos)
git clone --depth 1 https://github.com/user/repo.git

Pulling and Pushing

# Pull from current branch
git pull

# Pull from another branch
git pull origin branch-name

# Pull with rebase (cleaner history)
git pull --rebase

# Push and set upstream
git push -u origin branch-name

# Force push (dangerous - use with caution)
git push --force

# Safer force push
git push --force-with-lease

Moving Commits Between Branches

# On wrong branch
git reset HEAD~1 --soft
git stash

# Switch to correct branch
git checkout correct-branch
git stash pop
git commit -m "Your message"

Viewing History

Logs

git log
git log -5          # Last 5 commits
git log --oneline   # Compact format
git log -p          # See changes in each commit
git log --author="John Doe"  # By author
git log --since="2 weeks ago"  # Date range

Diffs

git diff                  # Unstaged changes
git diff --staged         # Staged changes
git diff origin/main      # Compare with remote
git diff --name-only      # List changed files

Advanced Operations

Stashing

git stash
git stash save "Work in progress"
git stash --staged        # Stash only staged changes
git stash -u              # Include untracked files
git stash list
git stash pop
git stash apply stash@{2}
git stash drop stash@{0}  # Delete a stash
git stash clear           # Clear all stashes

Configuration

git config --list
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global color.ui auto

Removing Files

# Remove file from Git (keep locally)
git rm --cached filename.js

# Remove directory recursively
git rm -r directory-name

Common Workflows

Starting a New Feature

# 1. Update main
git checkout main
git pull origin main

# 2. Create feature branch
git checkout -b feature/new-feature

# 3. Make changes and commit
git add .
git commit -m "Add feature"

# 4. Push
git push -u origin feature/new-feature

Fixing: Committed to Wrong Branch

# On wrong branch
git reset HEAD~1 --soft
git stash

# Switch to correct branch
git checkout correct-branch
git stash pop
git commit -m "Your message"

Resolving Merge Conflicts

# 1. Pull latest changes
git pull origin main

# 2. Conflicts appear
# CONFLICT (content): Merge conflict in src/app.js

# 3. Edit conflicted files
# ... resolve conflicts ...

# 4. Stage resolved files
git add src/app.js

# 5. Complete the merge
git commit -m "Resolve merge conflicts"

Modern Git (2.23+)

Git 2.23 (August 2019) introduced clearer commands to replace confusing ones.

The Problem with git checkout

git checkout does three different things:

  1. Switch branches: git checkout branch-name
  2. Restore files: git checkout -- filename
  3. Create and switch: git checkout -b new-branch

This is confusing! The new commands are clearer.

Old vs. New Syntax

TaskOld CommandNew Command (Git 2.23+)
Switch to existing branchgit checkout branchgit switch branch
Create and switch to new branchgit checkout -b branchgit switch -c branch
Discard changes in filegit checkout -- filegit restore file
Unstage filegit reset HEAD filegit restore --staged file

Git Terminology

TermDefinition
HEADPointer to current commit
originDefault remote repository
main/masterDefault branch name
upstreamOriginal repository (for forks)
stagedChanges ready to commit
commitSnapshot of code
branchParallel version of code
mergeCombine two branches
rebaseRewrite commit history
stashTemporarily save changes
cloneCopy repository locally
forkCopy someone’s repository

Frequently Asked Questions

What does git checkout -b do?

git checkout -b branch-name creates a new branch and immediately switches to it. The modern equivalent is git switch -c branch-name.

How do I unstage a file?

Modern: git restore --staged filename
Old: git reset HEAD filename

How to check current branch?

git status or git branch --show-current

How do I delete a commit?

Keep changes: git reset --soft HEAD~1
Discard: git reset --hard HEAD~1

How do I clone a specific branch?

git clone -b branch-name https://github.com/user/repo.git

What’s the difference between git reset and git revert?

git reset: Moves the branch pointer backward, rewrites history. ⚠️ Don’t use on pushed commits.
git revert: Creates a new commit that undoes changes. ✅ Safe for pushed commits.

How do I discard all local changes?

# Discard unstaged changes
git restore .

# Discard all changes
git reset --hard HEAD

# Match remote exactly
git fetch origin
git reset --hard origin/main

The Bottom Line

Git is powerful but complex. You don’t need to memorize every command—you just need a reliable reference.

Bookmark this page. The next time you’re stuck on Git syntax, you’ll have the answer in seconds.

Most-Used Commands (In Order)

  1. git status – Check what’s changed
  2. git checkout -b / git switch -c – Create and switch to a branch
  3. git add . – Stage all changes
  4. git commit -m – Commit with a message
  5. git push – Push to remote
  6. git pull – Pull from remote
  7. git reset --hard – Discard all changes
  8. git stash – Save work in progress

Master these 8, and you’ll handle 90% of your daily Git work.