Git

Git Commands Cheat Sheet

The daily Git command set on one page: the status-add-commit loop, branching, remotes, undo at every stage, stash, and history archaeology.

Gitversion controlcheat sheetreferencedeveloper tools

The mental model (one line)

Working directory → git add → staging area → git commit → repository. git status shows all three zones; run it whenever you're unsure.

One-time setup

CommandWhat it does
git config --global user.name "Name"Identity stamped on your commits
git config --global user.email "email"Ditto
git config --global init.defaultBranch mainNew repos start on main (matching the hosting services)
git config --listSee current settings

Daily loop

CommandWhat it does
git statusWhere is everything? Your most-typed command
git diffUnstaged changes, line by line
git diff --stagedWhat's on the loading dock, about to be committed
git add file / git add .Stage a file / everything
git add -pStage hunk by hunk — commit one story at a time
git commit -m "message"Seal the snapshot
git commit -am "message"Stage all tracked changes and commit in one go
git log --onelineHistory, one line per commit

Branching

CommandWhat it does
git branchList branches (* marks where you are)
git switch -c nameCreate a branch and move onto it
git switch nameMove to an existing branch
git merge nameMerge name into the branch you're on
git branch -d nameDelete a merged branch label
git branch -D nameForce-delete an unmerged one (you're sure)

Conflict? Git marks competing versions in the file with <<<<<<< / ======= / >>>>>>>. Edit the file to what's right, remove the markers, then git add it and git commit. It's a question, not an error.

Remotes

CommandWhat it does
git clone urlCopy an existing remote repo locally
git remote add origin urlConnect a local repo to a remote
git remote -vWhich remotes are configured?
git push -u origin mainFirst push (links the branches)
git push / git pullSend / fetch-and-merge commits
git fetchDownload remote history without merging — look before you leap

Undo — match the command to the zone

SituationCommand
Edited a file, want it back to last commitgit restore file ⚠ change is gone for good
Staged too much, keep the editsgit restore --staged file
Wrong message on the commit I just madegit commit --amend -m "better" (local only)
Forgot a file in the last commitgit add file then git commit --amend --no-edit
Undo a commit that's already pushedgit revert <hash> — new commit that reverses it
Undo a merge commitgit revert -m 1 HEAD
"I think I lost work"git reflog — everywhere your branches have pointed; committed work is recoverable

Rule of thumb: revert for anything pushed (adds history), restore/amend for local-only slips (rewrites it).

Stash — pocket your work

CommandWhat it does
git stashShelve uncommitted changes, get a clean tree
git stash listWhat's shelved
git stash popBring the latest stash back and drop it
git stash applyBring it back but keep the stash

History archaeology

CommandWhat it does
git log --oneline --graph --allThe branch structure, drawn
git log -p fileEvery change ever made to one file
git log --grep="text"Find commits by message
git blame fileWho last touched each line, and in which commit
git show <hash>Everything about one commit
git tag v1.0Name a commit permanently (releases)

.gitignore essentials

One pattern per line; add the file in your first commit:

node_modules/
.env
*.log
dist/
__pycache__/
.DS_Store

Already-tracked files ignore the ignore — untrack with git rm --cached file first.

The safety rules

  • git status before and after anything you're unsure about.
  • Secrets never enter history. A pushed key is a compromised key — rotate it; .gitignore from commit one.
  • git restore (without --staged) permanently discards edits — read the filename twice.
  • Never rewrite pushed history (--amend, rebase) on shared branches; revert instead.
  • Pull before you start, push when tests pass, commit small with honest messages.

Go deeper: every command here is taught with the mental model behind it in Git Essentials — three zones, and everything else follows.