←Back to Guides
๐ŸŒฟGit & Version Controlโฑ๏ธ 11 min read•Updated October 2026

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.

#Git#GitHub#Version Control#DevOps#Productivity#Developer#CLI

1. Intelligent Staging & Atomic Commits

Craft clean commit histories using interactive chunk staging instead of blindly running `git add .`:
Stage Changes Selectively by Chunk / Patch
git add -p

Interactively reviews diff chunks (hunks) allowing you to stage only related changes.

Amend Last Commit Without Changing Message
git add overlooked-file.ts
git commit --amend --no-edit

Silently absorbs staged files into the previous commit without creating a duplicate commit.

Compact Two-Line Status Output
git status -sb

Displays current branch tracking info and staged/unstaged files in compact notation.

2. The Git Emergency Room (How to Undo Anything)

Every developer makes mistakes. Git never actually deletes commits immediately; it keeps unreferenced commits in the reflog for up to 30 days. **Emergency Undo Scenarios:**
Undo Last Commit But Keep Changes Staged
git reset --soft HEAD~1

Rewinds the commit pointer by 1 while keeping your code changes safely in the staging index.

Unstage a File Accidentally Added
git restore --staged sensitive-secret.env

Removes the file from the staging area while leaving your disk contents untouched.

Discard All Uncommitted Disk Modifications
git restore .

Reverts all modified tracked files in the working directory back to the last commit.

Recover Deleted Commits or Branches with Reflog
# 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

Keep linear, readable commit histories by rebasing feature branches instead of cluttering history with unnecessary merge bubbles:
Switch to New Feature Branch
git switch -c feature/auth-passkeys

Modern replacement for `git checkout -b`.

Interactive Rebase Last 3 Commits (Squash / Reword)
git rebase -i HEAD~3

Opens an editor allowing you to `squash` (combine) or `reword` dirty commits into one clean release commit.

Rebase Feature Branch on Updated Main
git fetch origin
git rebase origin/main

Replays your local commits cleanly on top of the latest upstream main branch.

4. Stashing & Context Switching

Save dirty work-in-progress code without committing when an urgent bug fix is requested:
Stash All Changes Including Untracked Files
git stash -u -m "WIP: payment webhook refactor"

Saves your working directory and new untracked files with a descriptive label.

Restore and Remove the Most Recent Stash
git stash pop

Re-applies saved changes and drops them from the stash stack.

View Diff of Stashed Code Without Applying
git stash show -p stash@{0}

Inspects the full unified diff of a stash snapshot.

๐ŸŽ“Explore More Guides

โœจ