// cheat sheet

Git Cheat Sheet

The Git commands you reach for in real day-to-day work — clean branching workflows, rebases that don't lose history, cherry-picks across branches, stashes, resets, and the recovery commands that save you when a colleague force-pushes.

Quick Reference

  • git switch -c feature/x — create + switch branch
  • git pull --rebase — keep history linear
  • git stash push -m 'wip' — park work fast
  • git reflog — recover lost commits
  • git restore --staged <file> — unstage without losing work

Learning Path

Recommended order

  1. 1.Beginner
  2. 2.Intermediate
  3. 3.Advanced

Prerequisites

  • A Git repo on your machine
  • Basic shell

Skills you will learn

  • Safe branching
  • Interactive rebase
  • Conflict resolution
  • Recovery from mistakes

Estimated time

1 hour to scan; you'll keep this open for years.

Branching

git switch -c feature/order-export       # new branch
git switch main                          # switch
git branch -vv                           # branches with upstream
git push -u origin feature/order-export  # publish + track
git branch -d feature/order-export       # delete local
git push origin :feature/order-export    # delete remote

Merging

git switch main
git pull --rebase
git merge --no-ff feature/order-export   # preserve merge commit
git merge --squash feature/order-export  # single commit on main

Rebasing

# Pull with rebase — keep history linear
git config --global pull.rebase true

# Interactive rebase: clean up the last 5 commits
git rebase -i HEAD~5
# pick / reword / squash / fixup / drop

# Rebase a feature branch on the latest main
git switch feature/order-export
git fetch origin
git rebase origin/main

Cherry-pick

git cherry-pick <sha>          # apply commit on current branch
git cherry-pick A^..B          # range, exclusive of A
git cherry-pick --continue     # after resolving conflicts

Stash

git stash push -m "wip: order export"
git stash list
git stash show -p stash@{0}
git stash pop                 # apply + remove
git stash apply stash@{1}     # apply, keep

Reset

git reset --soft HEAD~1     # uncommit, keep changes staged
git reset --mixed HEAD~1    # uncommit, keep changes unstaged
git reset --hard HEAD~1     # uncommit + discard changes (danger)

git restore --staged file   # unstage
git restore file            # discard local changes

Recovery commands

git reflog                       # every HEAD movement
git checkout <reflog-sha>        # jump to lost commit
git branch rescue <reflog-sha>   # save it

# Recover a deleted branch
git reflog show --no-abbrev | grep <branch>
git branch <branch> <sha>

# Undo a force-push (if you have the old sha)
git update-ref refs/heads/main <old-sha>

Common Mistakes

  • !git push --force on shared branches — use --force-with-lease.
  • !Rebasing public branches that others have pulled.
  • !Committing secrets — use git-secrets / pre-commit hooks.
  • !Living on a long-lived feature branch and merging once a month.

Production Tips

  • Adopt a trunk-based workflow: short branches, daily merges.
  • Configure `pull.rebase = true` and `rebase.autoStash = true` globally.
  • Add a pre-push hook running tests and a leaked-secret scan.
  • Tag releases (`git tag -s v1.4.0`) and treat the tag as the artifact source of truth.

Further Reading

Frequently Asked Questions

How should I use this cheat sheet?

Skim once end-to-end, then keep it open in a pinned tab. Copy a snippet, adapt it to your project, and refer back when memory fails.

Is this cheat sheet up to date?

It's maintained against the latest stable releases in 2026 and revised when commands or APIs change meaningfully.