All cheat sheets

Git Cheat Sheet

Essential Git commands: setup, staging, branching, merging, undo, and remote workflows.

Version Control
git
vcs
version-control

Git records snapshots of a project and lets you move those snapshots between branches and remotes. Keep commits focused, inspect changes before committing, and choose undo commands according to whether history is already shared.

Setup and starting a repository

Configure identity once per machine or override it per repository. Set the default branch explicitly, then initialize a directory or clone an existing repository.

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

git init project
cd project
git clone https://example.com/team/project.git
Table
CommandPurpose
git config --listShow effective configuration.
git config --show-origin --listShow where each setting came from.
git statusSummarize the worktree and staging area.
git help <command>Open documentation for a command.

Stage and commit

The index is the proposed next snapshot. Review both unstaged and staged changes before creating a commit.

bash
git status --short
git diff
git add src/app.ts README.md
git diff --cached
git commit -m "Add request validation"
Table
CommandEffect
git add fileStage a path.
git add -pStage selected hunks interactively.
git add -AStage additions, modifications, and deletions.
git restore --staged fileUnstage without changing file contents.
git commit --amendReplace the latest local commit.
git diff --statShow a compact change summary.

Branches and navigation

Branches are movable names pointing at commits. switch is the purpose-built command for changing branches; checkout remains widely supported and also handles paths.

bash
git branch

git switch -c feature/search
# edit, stage, and commit work

git switch main
git branch --delete feature/search
Table
CommandPurpose
git branch nameCreate a branch without switching.
git switch nameSwitch to an existing branch.
git switch -c nameCreate and switch.
git branch -m old newRename a branch.
git branch -vvShow tracking branches and last commits.
git log --graph --oneline --allVisualize branch history.

Merge and rebase

Merging preserves branch topology with a merge commit when needed. Rebasing replays commits on a new base and produces a linear history; do not rebase commits others are already building on.

bash
git switch main
git pull --ff-only
git merge --no-ff feature/search
bash
git switch feature/search
git fetch origin
git rebase origin/main
# Resolve conflicts, then:
git add resolved-file
git rebase --continue
Table
SituationCommand
Abort a mergegit merge --abort
Abort a rebasegit rebase --abort
Continue after conflictgit add ... && git merge --continue
Preview merge resultgit diff main...feature/search

Undoing work safely

Use restore for files, reset for moving local branch pointers, and revert for a new inverse commit. The last option is safest for public history.

bash
git restore path/to/file
git restore --source HEAD~1 -- path/to/file
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert <commit>
Table
CommandChanges commit history?Typical use
git restore fileNoDiscard working-tree edits.
git reset --soft HEAD~1Yes, keeps changes stagedRedo the last commit.
git reset --hard HEAD~1Yes, discards tracked changesLocal-only recovery when certain.
git revert <commit>Adds an inverse commitUndo a pushed commit.

Remotes, inspection, and stash

Fetch downloads remote references without changing your branch. Pull combines fetch with merge or rebase according to configuration; push publishes local commits.

bash
git remote -v
git remote add origin https://example.com/team/project.git
git fetch --prune origin
git pull --rebase origin main
git push -u origin feature/search
bash
git log --oneline --decorate -10
git show --stat HEAD
git stash push -m "wip search UI"
git stash list
git stash pop

References