← All cheatsheets

Git

Git Commands — branch, merge, rebase, and recover

A deep working reference for everyday and recovery-grade Git: inspect history, branch and switch, choose merge vs rebase, squash and reword with interactive rebase, stash, reset vs revert, cherry-pick, bisect a bad commit, and undo almost anything with reflog. Destructive operations are flagged.

Docs-checkedgitbranchingrebaseresetreflog

Quick reference — reach for these first

Branch off safelygit switch -c feature/NAMECreate a new branch and move onto it in one step (modern replacement for git checkout -b).
Clean up historygit rebase -i HEAD~NSquash, reword, reorder, or drop the last N commits. HISTORY REWRITE — only on unpushed/local commits.
Discard local changesgit reset --hard ORIGIN/BRANCHDESTRUCTIVE: throws away all uncommitted work and moves the branch pointer. No undo except via reflog.
Undo a shared commitgit revert COMMIT_SHASafe inverse: creates a NEW commit that undoes COMMIT_SHA. Use this on pushed/shared branches instead of reset.
Recover anythinggit reflogShows where HEAD has pointed recently. Find a lost SHA here after a bad reset/rebase, then git reset --hard that SHA.

Merge vs Rebase: how each integrates feature into main

Merge versus rebaseMerge keeps both branch lines and adds a merge commit; rebase replays feature commits onto main as a straight line.MERGE — preserves branch history, adds merge commit MMABC mainDE featureREBASE — replays D,E onto C as a straight line (no M)ABC mainD'E' feature

Text equivalent: Two timelines. Top, merge: main commits A-B-C and feature commits D-E branching from B, joined by a new merge commit M on main, preserving both branch lines. Bottom, rebase: the same feature commits D and E are replayed as D-prime and E-prime directly on top of main's latest commit C, producing one straight line with no merge commit.

Common tasks

Task · Command · Verify

TaskCommandVerify
See compact history as a graphgit log --oneline --graph --decorate --allOne commit per line with branch/tag labels and ASCII branch lines.
Check working-tree and staging stategit status --short --branchBranch line plus XY status codes; empty list means a clean tree.
Create and switch to a new branchgit switch -c feature/SLUGgit branch --show-current prints feature/SLUG.
Switch back to an existing branchgit switch maingit status shows 'On branch main'.
Merge a feature branch into maingit switch main && git merge feature/SLUGgit log --oneline --graph shows a merge commit (or a fast-forward if linear).
Rebase a feature branch onto latest maingit switch feature/SLUG && git rebase maingit log --oneline shows feature commits sitting on top of main's tip.
Squash/reword recent commits — HISTORY REWRITEgit rebase -i HEAD~3Editor opens with pick/squash/reword lines; after saving, git log shows the condensed history.
Stash work-in-progress and restore itgit stash push -m "WIP" && git stash popgit status clean after push; changes reappear after pop and stash is dropped.
Unstage / undo last commit but keep changesgit reset --soft HEAD~1The commit is gone from git log but its changes are staged (git status shows them).
Discard everything back to last commit — DESTRUCTIVEgit reset --hard HEADgit status reports 'nothing to commit, working tree clean'; local edits are gone.
Safely undo a pushed commitgit revert COMMIT_SHAA new 'Revert ...' commit appears; the change is reversed without rewriting history.
Copy one commit onto the current branchgit cherry-pick COMMIT_SHAgit log --oneline shows the picked commit now on this branch.
Find the commit that introduced a buggit bisect start && git bisect bad && git bisect good TAG_OR_SHAGit checks out midpoints; after you mark each, it prints 'FIRST_BAD_SHA is the first bad commit'. Run git bisect reset when done.
Recover a lost commit after a bad resetgit reflogThe lost SHA appears in the list; git reset --hard SHA restores it.

Move a commit made on the wrong branch to the right one

recovery

1
You meant to commit on feature/login but you were on main. Confirm the stray commit and copy its SHA.
git log --oneline -3
# 9f3c1a2 (HEAD -> main) feat: add login form
# 4b7e0d1 docs: update readme
# a1c9f55 chore: bump deps
2
Switch to the branch the commit should live on.
git switch feature/login
# Switched to branch 'feature/login'
3
Copy the commit onto this branch.
git cherry-pick 9f3c1a2
# [feature/login 7d2b9ee] feat: add login form
4
Go back to main, which still has the mistaken commit.
git switch main
# Switched to branch 'main'
5
Remove the stray commit from main — DESTRUCTIVE, rewrites main's tip. Safe here only because main is local/unpushed; on a shared main use git revert instead.
git reset --hard HEAD~1
# HEAD is now at 4b7e0d1 docs: update readme
6
Verify: the commit is gone from main and present on feature/login.
git log --oneline -1 main && git log --oneline -1 feature/login
# 4b7e0d1 docs: update readme
# 7d2b9ee feat: add login form

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
"You are in 'detached HEAD' state" after checking out a SHA or taggit status (top line names the detached state and current SHA)HEAD points at a commit, not a branch; new commits here belong to no branch and can be garbage-collected.To keep work, git switch -c new-branch; to discard and return, git switch main.
Merge or rebase halts with 'CONFLICT (content)' and unmerged pathsgit status (lists 'Unmerged paths') and git diff to see the <<<<<<< markersBoth sides changed the same lines; Git cannot auto-combine them.Edit each file, remove conflict markers, git add FILE, then git merge --continue (or git rebase --continue). Bail out with git merge --abort / git rebase --abort.
push rejected: 'Updates were rejected ... non-fast-forward'git status and git log --oneline origin/BRANCH..HEAD vs HEAD..origin/BRANCHThe remote has commits you do not have locally; your push would overwrite them.git pull --rebase to replay your commits on top, resolve any conflicts, then push. Do NOT reach for push --force on a shared branch — HISTORY REWRITE that can drop teammates' commits.
Ran git reset --hard and lost commits/workgit reflog (recent HEAD positions are listed with SHAs)The old commits are unreferenced but still in the object store until garbage collection.Find the pre-reset SHA in the reflog and git reset --hard SHA (or git cherry-pick it) to restore. Uncommitted edits that were never staged are not recoverable.
Committed to the wrong branchgit log --oneline -3 on the current branch to copy the stray commit SHAThe commit landed on the branch you were on, not the intended one.git switch right-branch; git cherry-pick SHA; git switch wrong-branch; git reset --hard HEAD~1. Reset step is DESTRUCTIVE — confirm nothing else was added first.
'fatal: not a git repository' when running git commandspwd and git rev-parse --is-inside-work-treeThe current directory (and its parents) contain no .git folder.cd into the project root, or run git init / git clone URL to create or fetch the repository.

References

Modern Git (2.23+) is assumed so git switch/restore are available; older workflows use git checkout -b and git checkout equivalently. CAPS tokens (NAME, SLUG, SHA, BRANCH, TAG, FILE, N) are placeholders to replace. Destructive operations (reset --hard, interactive rebase, push --force, rebasing pushed commits) are labeled; apply them only to local/unpushed history.