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.
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
| Command | What 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 main | New repos start on main (matching the hosting services) |
git config --list | See current settings |
Daily loop
| Command | What it does |
|---|---|
git status | Where is everything? Your most-typed command |
git diff | Unstaged changes, line by line |
git diff --staged | What's on the loading dock, about to be committed |
git add file / git add . | Stage a file / everything |
git add -p | Stage 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 --oneline | History, one line per commit |
Branching
| Command | What it does |
|---|---|
git branch | List branches (* marks where you are) |
git switch -c name | Create a branch and move onto it |
git switch name | Move to an existing branch |
git merge name | Merge name into the branch you're on |
git branch -d name | Delete a merged branch label |
git branch -D name | Force-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
| Command | What it does |
|---|---|
git clone url | Copy an existing remote repo locally |
git remote add origin url | Connect a local repo to a remote |
git remote -v | Which remotes are configured? |
git push -u origin main | First push (links the branches) |
git push / git pull | Send / fetch-and-merge commits |
git fetch | Download remote history without merging — look before you leap |
Undo — match the command to the zone
| Situation | Command |
|---|---|
| Edited a file, want it back to last commit | git restore file ⚠ change is gone for good |
| Staged too much, keep the edits | git restore --staged file |
| Wrong message on the commit I just made | git commit --amend -m "better" (local only) |
| Forgot a file in the last commit | git add file then git commit --amend --no-edit |
| Undo a commit that's already pushed | git revert <hash> — new commit that reverses it |
| Undo a merge commit | git 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
| Command | What it does |
|---|---|
git stash | Shelve uncommitted changes, get a clean tree |
git stash list | What's shelved |
git stash pop | Bring the latest stash back and drop it |
git stash apply | Bring it back but keep the stash |
History archaeology
| Command | What it does |
|---|---|
git log --oneline --graph --all | The branch structure, drawn |
git log -p file | Every change ever made to one file |
git log --grep="text" | Find commits by message |
git blame file | Who last touched each line, and in which commit |
git show <hash> | Everything about one commit |
git tag v1.0 | Name 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 statusbefore and after anything you're unsure about.- Secrets never enter history. A pushed key is a compromised key — rotate it;
.gitignorefrom commit one. git restore(without--staged) permanently discards edits — read the filename twice.- Never rewrite pushed history (
--amend, rebase) on shared branches;revertinstead. - 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.