Git Commands Cheat Sheet
Branching, merging, rebasing, stashing, and the recovery commands that get you out of a bad state without losing work.
Branching & Merging
git checkout -b feature/xCreate and switch to a new branchgit switch -c feature/xModern equivalent of checkout -bgit merge feature/xMerge a branch into the current branchgit branch -d feature/xDelete a branch that's been mergedgit branch -D feature/xForce-delete a branch, merged or notgit branch -vvList branches with their upstream tracking statusRebasing & History
git rebase mainReplay current branch's commits on top of maingit rebase -i HEAD~5Interactively squash/reorder/edit the last 5 commitsgit cherry-pick <sha>Apply a single commit from another branchgit log --oneline --graph --allCompact visual history of all branchesgit blame file.jsShow which commit last changed each lineUndoing Things
git restore file.jsDiscard uncommitted changes to a filegit reset --soft HEAD~1Undo last commit, keep changes stagedgit reset --hard HEAD~1Undo last commit, discard changes entirelygit revert <sha>Create a new commit that undoes a previous commit (safe on shared history)git reflogShow every HEAD movement — recovers 'lost' commits after a bad resetStashing & Remotes
git stashTemporarily shelve uncommitted changesgit stash popReapply and remove the most recent stashgit fetch originDownload remote changes without merginggit pull --rebaseFetch and rebase local commits on top instead of merginggit push -u origin feature/xPush a new branch and set its upstream