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.
Quick reference — reach for these first
git switch -c feature/NAMECreate a new branch and move onto it in one step (modern replacement for git checkout -b).git rebase -i HEAD~NSquash, reword, reorder, or drop the last N commits. HISTORY REWRITE — only on unpushed/local commits.git reset --hard ORIGIN/BRANCHDESTRUCTIVE: throws away all uncommitted work and moves the branch pointer. No undo except via reflog.git revert COMMIT_SHASafe inverse: creates a NEW commit that undoes COMMIT_SHA. Use this on pushed/shared branches instead of reset.git 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
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
| Task | Command | Verify |
|---|---|---|
| See compact history as a graph | git log --oneline --graph --decorate --all | One commit per line with branch/tag labels and ASCII branch lines. |
| Check working-tree and staging state | git status --short --branch | Branch line plus XY status codes; empty list means a clean tree. |
| Create and switch to a new branch | git switch -c feature/SLUG | git branch --show-current prints feature/SLUG. |
| Switch back to an existing branch | git switch main | git status shows 'On branch main'. |
| Merge a feature branch into main | git switch main && git merge feature/SLUG | git log --oneline --graph shows a merge commit (or a fast-forward if linear). |
| Rebase a feature branch onto latest main | git switch feature/SLUG && git rebase main | git log --oneline shows feature commits sitting on top of main's tip. |
| Squash/reword recent commits — HISTORY REWRITE | git rebase -i HEAD~3 | Editor opens with pick/squash/reword lines; after saving, git log shows the condensed history. |
| Stash work-in-progress and restore it | git stash push -m "WIP" && git stash pop | git status clean after push; changes reappear after pop and stash is dropped. |
| Unstage / undo last commit but keep changes | git reset --soft HEAD~1 | The commit is gone from git log but its changes are staged (git status shows them). |
| Discard everything back to last commit — DESTRUCTIVE | git reset --hard HEAD | git status reports 'nothing to commit, working tree clean'; local edits are gone. |
| Safely undo a pushed commit | git revert COMMIT_SHA | A new 'Revert ...' commit appears; the change is reversed without rewriting history. |
| Copy one commit onto the current branch | git cherry-pick COMMIT_SHA | git log --oneline shows the picked commit now on this branch. |
| Find the commit that introduced a bug | git bisect start && git bisect bad && git bisect good TAG_OR_SHA | Git 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 reset | git reflog | The 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
git log --oneline -3
# 9f3c1a2 (HEAD -> main) feat: add login form
# 4b7e0d1 docs: update readme
# a1c9f55 chore: bump depsgit switch feature/login
# Switched to branch 'feature/login'git cherry-pick 9f3c1a2
# [feature/login 7d2b9ee] feat: add login formgit switch main
# Switched to branch 'main'git reset --hard HEAD~1
# HEAD is now at 4b7e0d1 docs: update readmegit log --oneline -1 main && git log --oneline -1 feature/login
# 4b7e0d1 docs: update readme
# 7d2b9ee feat: add login formTroubleshooting — symptom → first check → next check
| Symptom | First check | Establishes | Next check |
|---|---|---|---|
| "You are in 'detached HEAD' state" after checking out a SHA or tag | git 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 paths | git status (lists 'Unmerged paths') and git diff to see the <<<<<<< markers | Both 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/BRANCH | The 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/work | git 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 branch | git log --oneline -3 on the current branch to copy the stray commit SHA | The 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 commands | pwd and git rev-parse --is-inside-work-tree | The 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
- Pro Git Book — Branching & Rebasinggit-scm.com
- git-reset Reference Manualgit-scm.com
- git-reflog Reference Manualgit-scm.com
- git-bisect Reference Manualgit-scm.com
- git-switch Reference Manualgit-scm.com
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.