Git Version Control Master Cheat Sheet: Rebasing, Stashing & Emergency Undos
The ultimate survival cheat sheet for Git version control: interactive staging, squashing commits, stashing untracked files, and the Emergency Room guide to undoing any mistake with Reflog.
1. Intelligent Staging & Atomic Commits
git add -pInteractively reviews diff chunks (hunks) allowing you to stage only related changes.
git add overlooked-file.ts
git commit --amend --no-editSilently absorbs staged files into the previous commit without creating a duplicate commit.
git status -sbDisplays current branch tracking info and staged/unstaged files in compact notation.
2. The Git Emergency Room (How to Undo Anything)
git reset --soft HEAD~1Rewinds the commit pointer by 1 while keeping your code changes safely in the staging index.
git restore --staged sensitive-secret.envRemoves the file from the staging area while leaving your disk contents untouched.
git restore .Reverts all modified tracked files in the working directory back to the last commit.
# 1. Find lost commit SHA:
git reflog
# 2. Restore state:
git reset --hard HEAD@{2}Lists every action taken in your local repository, allowing complete recovery of lost commits.
3. Clean Branching & Interactive Rebasing
git switch -c feature/auth-passkeysModern replacement for `git checkout -b`.
git rebase -i HEAD~3Opens an editor allowing you to `squash` (combine) or `reword` dirty commits into one clean release commit.
git fetch origin
git rebase origin/mainReplays your local commits cleanly on top of the latest upstream main branch.
4. Stashing & Context Switching
git stash -u -m "WIP: payment webhook refactor"Saves your working directory and new untracked files with a descriptive label.
git stash popRe-applies saved changes and drops them from the stash stack.
git stash show -p stash@{0}Inspects the full unified diff of a stash snapshot.
Related Free Online Tools
Put these techniques into practice immediately with our privacy-first browser utilities: