Stash work in progress, undo commits with reset and revert, inspect diffs, recover from detached HEAD, cherry-pick commits, rebase, and squash messy history.
Why: lets you switch tasks without committing half-finished work.
Save uncommitted changes and clean the working directory
git stash push -m "Message"git status -sSee what's stashed
git stash listBring the most recent stash back and remove it from the list
git stash popOr apply without removing it
git stash apply stash@{0}Or just remove it from the list
git stash drop stash@{0}Why: restore is the modern, focused way to undo changes in the working directory or staging area, without touching commit history.
Discard unstaged changes in a file (back to last commit)
git restore "file"Discard unstaged changes everywhere
git restore .Unstage a file, keeping its changes in the working directory
git restore --staged "file"Why: --soft keeps your changes staged, --mixed unstages them, --hard discards them completely. Only use --hard if you are sure.
Undo the last commit, keep its changes staged
git reset --soft HEAD~1Undo the last commit, keep its changes in the working directory
git reset --mixed HEAD~1Undo the last commit and throw away its changes entirely
git reset --hard HEAD~1Why: revert creates a new commit that undoes a previous one — safe to use on history that has already been pushed.
Create a new commit that reverses the last commit
git revert HEADgit commit -m "Message"Unstaged changes vs the last commit
git diffStaged changes vs the last commit
git diff --stagedDifference between two commits
git diff "hash" "hash"Difference between two branches
git diff main..featureNote: checking out a commit instead of a branch puts you in 'detached HEAD' — fine for looking around, but create a branch before committing or your work can be lost.
Check out an old commit directly
git checkout "hash"To keep any work done here, create a branch from this point
git switch -c "branch-name"Otherwise, just go back to where you were
git switch mainWhy: copies a single commit from one branch onto another, without merging everything else.
Apply one commit from another branch onto the current branch
git cherry-pick "hash"Apply several commits in order
git cherry-pick "hash" "hash"If it conflicts, resolve, then:
git add "file"git cherry-pick --continueOr if you want you can abort
git cherry-pick --abortWhy: rebase replays your branch's commits on top of the latest main, producing a linear history instead of a merge commit.
git switch "feature-branch"git rebase mainResolve any conflicts as they appear
git add "file"git rebase --continueOr back out completely
git rebase --abortWhy: turns a messy series of "wip", "fix typo", "actually fix it" commits into one clean commit.
Interactively squash the last 3 commits into one
git rebase -i HEAD~3In the editor, change "pick" to "squash" (or "s") for the commits you want folded into the one above, then save.
Alternative: squash an entire branch into one commit on merge
git switch maingit merge --squash "feature-branch"git commit -m "Message"