This is the reference for the commands that come up in real work — not the full man page. Grouped by what you're trying to do, with the flags that matter.
Starting and cloning
git init — create a new repo in the current folder
git clone <url> — clone a remote repo
git clone <url> --depth 1 — shallow clone, only latest commit (faster for CI)
Branching
git checkout -b feature-name — create and switch to a new branch
git switch -c feature-name — same as above (newer syntax)
git branch -d feature-name — delete a local branch (safe — won't delete unmerged)
git branch -D feature-name — force delete even if unmerged
git add -p — interactively stage chunks (best way to make clean commits)
git commit -m "message" — commit with inline message
git commit --amend --no-edit — add staged changes to last commit without changing message
git commit --amend -m "new message" — fix last commit message
Stashing
git stash — stash all uncommitted changes
git stash pop — apply most recent stash and remove it
git stash list — see all stashes
git stash apply stash@{2} — apply a specific stash without removing it
Undoing changes
git restore filename — discard unstaged changes to a file
git reset HEAD~1 — undo last commit, keep changes staged
git reset --hard HEAD~1 — undo last commit, discard changes
git revert HEAD — safe undo: new commit that reverses last commit
git reflog — view full history of HEAD changes (lifesaver after force-push)
Complete Developer Toolkit
This Git cheat sheet works best alongside our Git error guide — use the cheat sheet to find the right command and the error guide when something goes wrong. Our diff checker replicates git diff in the browser — paste two versions of a file to visualize what changed without needing a terminal. For automating git operations in CI/CD pipelines, our cron generator schedules automated git pulls or deployments, and our Linux terminal cheat sheet covers the shell commands that surround those git operations.
When reviewing pull requests that include config file changes, our JSON formatter validates modified package.json, tsconfig.json, or API config files. The regex tester helps you write git log --grep patterns and .gitignore glob expressions. For repos that include cron jobs in their deployment process, our crontab descriptor decodes those schedule expressions into plain English. Our SSL expiry checker and DNS lookup diagnose the connection issues that cause git push failures to remote repositories.
Remote Workflows
Most Git work involves synchronizing with remotes:
Command
What It Does
git fetch origin
Download remote changes without merging
git pull --rebase
Fetch + rebase your commits on top of remote
git push -u origin feature/x
Push new branch and set upstream tracking
git push --force-with-lease
Force push safely — fails if remote changed since last fetch
git remote -v
List all remotes with URLs
git remote add upstream URL
Add upstream remote for forked repos
Rebasing and History Editing
Rebasing rewrites commit history. Use it to clean up before merging — never on shared branches.
Command
Purpose
git rebase -i HEAD~3
Interactive rebase last 3 commits (squash, reorder, edit)
git rebase main
Move current branch onto latest main
git commit --amend
Edit the most recent commit
git reset --soft HEAD~1
Undo last commit but keep changes staged
git cherry-pick abc1234
Apply a specific commit from another branch
Safety rule: Only rebase local/unpushed commits. Use --force-with-lease if you must rewrite pushed history.
Troubleshooting Common Git Problems
Problem
Fix
Accidentally committed secrets
git commit --amend to remove, then rotate the secret
Merge conflict in progress
Resolve in editor, then git add . && git merge --continue
Abort a merge/rebase
git merge --abort or git rebase --abort
Lost a commit after bad reset
git reflog to find SHA, then git cherry-pick SHA
Wrong branch, no commits yet
git stash && git checkout correct && git stash pop
See what changed in a commit
git show abc1234
FAQ
Merge preserves history exactly as it happened — you get a merge commit and both branch histories. Rebase replays your commits on top of another branch, giving a linear history. Rebase is cleaner but rewrites commit hashes, so never rebase commits that others have already pulled.
Use reset for local commits you haven't pushed — it rewrites history. Use revert for commits you've already pushed — it creates a new commit that undoes changes without rewriting history, so it's safe to push.
git show <hash> shows the diff for a specific commit. git log --oneline --graph gives a visual branch history. git diff main..feature-branch shows all differences between two branches.