Nine spreads. Each has a picture, the idea, and the exact commands, from your first commit to pull requests and fixing mistakes.
"Small commits on a branch, a pull request for review, rebase or merge into main, and I can always get back to a known-good state."
Git is the tool on your computer. GitHub is a website that hosts Git repositories and adds collaboration. Use the tabs or ← → keys.
The staging area lets you build a commit deliberately: choose which changes go together, even from the same file.
$ git config --global user.name "Your Name" $ git config --global user.email "you@example.com" $ git config --global init.defaultBranch main $ git init # start tracking this folder $ git status # what's changed? (use it constantly) $ git add index.html # stage one file (git add -p: by chunk) $ git commit -m "Add homepage" $ git log --oneline --graph --all
node_modules/, .env, build output"One logical change per commit, with a message that explains why."
$ git status -s # short status $ git diff # unstaged changes $ git diff --staged # what the next commit will contain $ git add -p # review each chunk, stage or skip $ git commit -m "Validate email on signup" $ git commit --amend # fix the last commit (unpushed only) $ git log --oneline -10 $ git show a1b2c3d # one commit in detail $ git blame app.js # who changed each line, and when $ git rm old.txt ; git mv a.js b.js
a1b2c3d"I always read git diff --staged before I commit."
A branch is cheap: just a pointer. Make one for every feature or fix, and keep main always working.
$ git branch # list (git branch -a for remotes too) $ git switch -c feature/login # create and move to it $ git switch main $ git merge feature/login # bring it into main $ git branch -d feature/login # delete after merging $ git switch feature/login ; git rebase main # replay onto latest main
Two branches edited the same lines. Git marks the file; you choose the final text.
<<<<<<< HEAD const limit = 10; ======= const limit = 25; >>>>>>> feature/login
# edit the file, delete the markers, keep the right code, then: $ git add file.js ; git merge --continue # or: git rebase --continue $ git merge --abort # bail out safely
"Short-lived branches, rebase onto main before the PR, and resolve conflicts in the editor, then re-run the tests."
# 1. create an empty repo on github.com (or: gh repo create) $ git remote add origin https://github.com/you/project.git $ git branch -M main $ git push -u origin main # -u remembers the upstream $ git remote -v
$ git clone https://github.com/org/project.git $ git fetch ; git pull --rebase $ git push
GitHub no longer accepts account passwords for git. Use HTTPS with the GitHub CLI or a credential manager, or SSH keys.
$ gh auth login # or SSH: $ ssh-keygen -t ed25519 -C "you@example.com" $ cat ~/.ssh/id_ed25519.pub # paste into GitHub → Settings → SSH keys $ ssh -T git@github.com
"Pull with rebase before pushing, and never commit secrets or tokens."
$ git switch -c fix/cart-total $ git commit -am "Fix rounding in cart total" $ git push -u origin fix/cart-total $ gh pr create --fill --body "Closes #42" $ gh pr status ; gh pr checks $ gh pr checkout 57 # try someone's PR locally $ gh pr merge --squash --delete-branch
| Merge button | Result |
|---|---|
| Merge commit | keeps every commit plus a merge commit |
| Squash and merge | one tidy commit on main (popular) |
| Rebase and merge | straight line, commits replayed |
"Small PR, clear description, green checks, and one approval before I squash-merge."
$ git restore file.js # discard unstaged edits (permanent!) $ git restore --staged file.js # unstage, keep the edits $ git commit --amend -m "Better msg" # fix last commit, unpushed only $ git reset --soft HEAD~1 # undo commit, keep changes staged $ git reset --hard HEAD~1 # undo commit AND delete the changes $ git revert a1b2c3d # new commit that reverses it (safe) $ git stash ; git stash pop # shelve work, bring it back $ git reflog # find a "lost" commit $ git switch -c rescue a1b2c3d # give it a branch again
--force-with-lease, and never on shared branches"On shared branches I revert. I only rewrite history on my own unpushed work."
feature/…, fix/…, chore/….feat: add search or fix: handle empty cart.$ git tag -a v1.2.0 -m "Release 1.2.0" $ git push origin v1.2.0 $ git log v1.1.0..v1.2.0 --oneline # what shipped $ git cherry-pick a1b2c3d # copy one commit onto this branch $ git bisect start ; git bisect bad ; git bisect good v1.1.0 # find the commit that broke it
"Protected main, conventional commits, tagged releases, and bisect when something regresses."
name: ci
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test$ gh issue create --title "Search is slow" --label bug $ gh issue list ; gh run list ; gh release create v1.2.0 --generate-notes $ gh repo view --web
"Every change links to an issue, CI must pass, and Dependabot keeps the dependencies fresh."
# "I committed to main by mistake" $ git branch fix/oops ; git reset --hard origin/main ; git switch fix/oops # "I need to undo a bad merge that's not pushed" $ git reset --hard ORIG_HEAD # "I want to see what's different from main" $ git diff main...HEAD ; git log main..HEAD --oneline # "Stop tracking a file but keep it on disk" $ git rm --cached .env ; echo ".env" >> .gitignore # "Clean untracked files" (preview first!) $ git clean -nd ; git clean -fd
git status. Don't guess.git branch backup is free insurance."I made a backup branch, then reset. If it goes wrong, the reflog has my old HEAD."