← Back to Blog

Git Cheat Sheet 2026: Every Command You Actually Need

Git has hundreds of commands, but you use maybe 20 of them daily. This cheat sheet covers every command you actually need, organized by workflow — with the dangerous ones clearly marked.

Setup and Configuration

# Identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Useful defaults
git config --global init.defaultBranch main
git config --global pull.rebase true          # rebase on pull instead of merge
git config --global fetch.prune true          # auto-remove deleted remote branches
git config --global diff.algorithm histogram  # better diff output
git config --global core.autocrlf input       # normalize line endings (macOS/Linux)

# Check all settings
git config --list --show-origin

Everyday Commands

# Clone a repo
git clone git@github.com:user/repo.git
git clone --depth 1 git@github.com:user/repo.git  # shallow clone (faster)

# Status and diff
git status                  # what changed?
git diff                    # unstaged changes
git diff --staged           # staged changes (about to be committed)
git diff main..feature      # diff between branches

# Stage and commit
git add file.js             # stage specific file
git add -p                  # stage interactively (hunk by hunk)
git commit -m "fix: resolve null pointer in auth middleware"
git commit --amend          # modify last commit (before push only!)

# Push and pull
git push origin main
git push -u origin feature  # push + set upstream
git pull                    # fetch + rebase (if configured)
git fetch --all             # download all remote changes without merging

Branching and Merging

# Create and switch
git checkout -b feature/auth-refactor    # old way
git switch -c feature/auth-refactor      # new way (Git 2.23+)

# List branches
git branch                  # local
git branch -r               # remote
git branch -a               # all
git branch --merged main    # branches already merged into main

# Merge
git merge feature           # merge feature into current branch
git merge --no-ff feature   # force a merge commit (no fast-forward)

# Delete branches
git branch -d feature       # safe delete (only if merged)
git branch -D feature       # force delete (even if not merged)
git push origin --delete feature  # delete remote branch

Rebase vs Merge: When to Use Each

Merge preserves history as it happened. Creates a merge commit. Use for merging feature branches into main when you want a clear record of when branches were integrated.

Rebase rewrites history to create a linear timeline. Use for keeping feature branches up to date with main before merging. Never rebase commits that have been pushed to a shared branch.

# Rebase feature branch onto latest main
git checkout feature
git rebase main

# Interactive rebase: squash, reorder, edit commits
git rebase -i HEAD~5        # rebase last 5 commits
# In the editor:
# pick   abc1234 Add user model
# squash def5678 Fix typo in user model    <-- squash into previous
# pick   ghi9012 Add user API endpoint

Undoing Mistakes

# Undo unstaged changes to a file
git checkout -- file.js     # old way
git restore file.js         # new way

# Unstage a file (keep changes)
git reset HEAD file.js      # old way
git restore --staged file.js # new way

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

# Undo last commit (keep changes unstaged)
git reset HEAD~1

# Undo last commit (DISCARD changes) ⚠️ DESTRUCTIVE
git reset --hard HEAD~1

# Undo a pushed commit (creates a new revert commit)
git revert abc1234          # safe for shared branches

# Recover deleted commits (git saves everything for 30 days)
git reflog                  # find the lost commit hash
git checkout abc1234        # check it out
git cherry-pick abc1234     # or cherry-pick it

The reflog is your safety net. Even after a reset --hard, your commits are recoverable for 30 days via git reflog. This is the most important Git command most developers do not know about.

Stashing

# Save work in progress
git stash                        # stash tracked files
git stash -u                     # include untracked files
git stash push -m "WIP: auth"    # with a description

# List and restore
git stash list
git stash pop                    # restore and delete stash
git stash apply stash@{2}        # restore without deleting
git stash drop stash@{0}         # delete a stash
git stash clear                  # delete all stashes

Cherry-Pick and Bisect

# Apply a specific commit to current branch
git cherry-pick abc1234
git cherry-pick abc1234 def5678  # multiple commits

# Find which commit introduced a bug
git bisect start
git bisect bad                   # current commit is bad
git bisect good v1.0.0           # this tag/commit was good
# Git checks out a middle commit. Test it, then:
git bisect good                  # or git bisect bad
# Repeat until Git identifies the exact commit
git bisect reset                 # done, go back to original HEAD

Log and History

# Useful log formats
git log --oneline --graph --all          # visual branch history
git log --since="2 weeks ago"            # recent commits
git log --author="Jane"                  # by author
git log -p file.js                       # changes to a specific file
git log -S "functionName"                # search for string in diffs (pickaxe)
git log --pretty=format:"%h %ad %s" --date=short  # custom format
git shortlog -sn                         # commit count by author

# Blame: who changed each line
git blame file.js
git blame -L 10,20 file.js              # specific line range

Commit Message Convention

Most teams use Conventional Commits to standardize messages and enable automated changelogs:

feat: add user authentication via OAuth2
fix: resolve null pointer in payment webhook handler
docs: update API documentation for v3 endpoints
refactor: extract email validation into shared utility
perf: optimize database query for user search (3x faster)
test: add integration tests for checkout flow
chore: upgrade dependencies to latest versions

The format is: type(optional scope): description. Keep the first line under 72 characters. Add a body for context when needed.

Generate .gitignore Files Instantly

Select your tech stack and generate a comprehensive .gitignore. Supports 500+ templates for languages, frameworks, and IDEs.

Open .gitignore Generator

The Bottom Line

Master these commands and you will handle 99% of Git workflows confidently. When in doubt: git status, git stash, git reflog. These three will get you out of almost any situation.

Related tools: .gitignore Generator, Diff Checker, Hash Generator (Git uses SHA-1/SHA-256 internally), and 50+ more free tools.