Gitverse

$ git help --cheatsheet

The Git cheatsheet

Every command you'll actually use, grouped by what you're trying to do. Click any command to copy it.

Where your changes live

Most commands just move changes between these four places.

Working folder

the files you edit

Staging area

draft of the next commit

Local repository

your commit history

Remote

GitHub, shared with others

  • git addWorking folder → Staging area
  • git commitStaging area → Local repository
  • git pushLocal repository → Remote
  • git fetchRemote → Local repository
  • git pullRemote → Working folder
  • git restore --stagedStaging area → Working folder
  • git switch / restoreLocal repository → Working folder

Setup

Once per computer. Your name and email go into every commit.

Start a project

Stage & commit

The everyday loop: edit → add → commit.

Explore history

Branches

A branch is just a movable label on a commit. They're cheap — use lots.

Merge & rebase

Remotes & GitHub

Undo

Pushed already? Prefer revert. Only rewrite commits nobody else has.

Stash

A drawer for half-finished work when you need a clean folder.

Tags & releases

Naming commits

Use these anywhere a command expects a commit.

HEAD
Where you are now
HEAD~1
One commit back (HEAD~3: three back)
HEAD^2
Second parent of a merge commit
a1b2c3d
Any commit, by the start of its hash
main..feature
Commits on feature that main lacks
origin/main
Last known state of main on GitHub
@{u}
The upstream of the current branch
HEAD@{2}
Where HEAD was two moves ago (reflog)

.gitignore patterns

Files Git should never track. One pattern per line.

*.log
Every .log file, anywhere
node_modules/
A folder (trailing slash = folders only)
/build
Only at the repository root
**/tmp
tmp at any depth
.env
Secrets — never commit them
!keep.log
Exception: do track this one

Oops! Get out of trouble

Common mistakes and the exact commands that fix them.

I committed to the wrong branch

  1. $
  2. $
  3. $

Only if that commit isn't pushed yet.

Typo in my last commit message

  1. $

I forgot a file in my last commit

  1. $
  2. $

Undo my last commit but keep the work

  1. $

I pushed a commit that broke things

  1. $
  2. $

Never rewrite history others already pulled.

I lost commits after a reset

  1. $
  2. $

Git keeps them for about 90 days.

This merge is a mess, get me out

  1. $

“You are in 'detached HEAD' state”

  1. $

Keeps any commits you made there. Or just git switch main.

Push rejected: remote has new work

  1. $
  2. $

Golden rules

  1. 1Commit small and often — one idea per commit.
  2. 2Write messages in the imperative: “Add login form”.
  3. 3Run git status before anything you're unsure about.
  4. 4Pull before you push.
  5. 5Never rewrite history that others already have.
  6. 6Work on a branch, not directly on main.