Developer ToolsUpdated: 2026-11-04

Essential Git Commands, Branching & Recovery Cheat Sheet

Quick lookup guide for git staging, interactive rebasing, stashing, undoing mistakes, and team branching workflows.

Git is the distributed version control system powering modern software engineering. From resolving tricky merge conflicts and cleaning up commit histories with interactive rebase, to rescuing accidentally deleted commits with reflog, keep this quick reference handy for your terminal sessions.

Configuration & Initialization

First-time setup and local repository initialization.

Set Global Git Identitybash
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
Configures default author identity across all local repositories.
Initialize New Repository or Clonebash
git init                     # Initialize git in current directory
git clone <repository-url>   # Clone remote repository locally
Creates a new local repository or downloads remote history.

Staging, Commits & History Inspection

Day-to-day workflow commands for tracking file changes and viewing commit logs.

CommandAction & Description
git statusDisplays modified, staged, and untracked files in the working tree.
git add -pInteractively stage specific hunks/patches rather than whole files.
git commit -m "feat: msg"Records snapshot of staged changes with a concise commit message.
git commit --amend --no-editAppends newly staged changes into the most recent commit without altering message.
git log --oneline --graphVisualizes branch topology and recent commits in compact single-line format.
git diff --stagedCompares staged changes against the previous commit snapshot.

Branching, Rebasing & Merging

Branch management and history manipulation.

Create & Switch Branchesbash
git switch -c feature/new-tool    # Modern: create & switch
git checkout -b feature/new-tool   # Classic equivalent
Creates a new feature branch and checks it out.
Interactive Rebase (Squash & Reorder Commits)bash
git fetch origin main
git rebase -i origin/main
Opens interactive editor to squash (s), edit (e), drop (d), or reword (r) local commits before pushing.
Cherry-Pick a Specific Commitbash
git cherry-pick <commit-hash>
Applies the changes from an isolated commit onto the current HEAD branch.

Stashing & Temporary Work

Save uncommitted work without committing so you can switch branches cleanly.

CommandUsage
git stash push -m "WIP: auth"Shelves modified files with a descriptive label.
git stash listInspects all saved stash snapshots.
git stash popRestores the most recent stash and removes it from the stash stack.
git stash apply stash@{1}Applies a specific stash index without deleting it.
git stash dropDiscards the topmost stash entry.

Undoing Changes & Disaster Recovery

Safely recover from mistakes without losing code.

Discard Working Directory Changesbash
git restore <file-path>       # Discard un-staged modifications
git restore --staged <file-path> # Unstage file keeping changes on disk
Modern clean syntax to restore modified files.
Soft vs Hard Resetbash
git reset --soft HEAD~1   # Undo commit, keeps all code staged
git reset --hard HEAD~1   # DANGER: permanently discards commit and changes
Soft reset preserves your code; hard reset wipes uncommitted modifications.
Rescue Deleted Commits with Reflogbash
git reflog
git reset --hard HEAD@{n}
Reflog records every HEAD movement, enabling recovery of deleted branches or botched rebases.

Interactive Client-Side Tools for this Guide

Test and run these patterns directly inside your browser with zero server uploads:

Frequently Asked Questions

When should I use Git Rebase vs Git Merge?

Use git merge when integrating shared public branches (e.g., merging a feature branch into main) to preserve true chronological history. Use git rebase on private local feature branches to keep a clean, linear commit history before opening a pull request.

How can I safely exit a stuck Git rebase or merge?

Run `git rebase --abort` or `git merge --abort`. This safely rolls your repository back to the exact state it was in before initiating the rebase or merge.