Cheat sheet · Git and GitHub From Zero

Git & GitHub cheat sheet

Everything you reach for in daily Git, from the first commit to undoing mistakes and working with a team on GitHub. Git stores snapshots (commits), and a branch is only a label that points at one.

Read the full series →

blogs · jp-blogs.vercel.app/cheatsheets/series/git-github

The model in 30 secondsLesson 0 →

  • A commit is a snapshot of the whole project, with a parent, an author, a message and a hash.
  • A branch is a movable label on a commit. HEAD says which branch (or commit) you are on.
  • Work moves through three places: working tree → staging area (add) → repository (commit).
  • Git compares by hash, so equal content has an equal hash, and history cannot change silently.
PlaceYou get there withUndo with
Working treeEdit a filegit restore FILE
Staging areagit add FILEgit restore --staged FILE
Repositorygit commitgit reset or git revert

Daily loopLesson 1 →

git status
What changed, what is staged
git add -p
Stage piece by piece (small commits)
git commit -m "msg"
Save the staged snapshot
git diff
Unstaged changes
git diff --staged
What the next commit will contain
git log --oneline --graph
Short history with branches
git log -S"text"
Find commits that added or removed text
git show HASH
One commit in full

Tip: Small commits with a clear message make every later tool (bisect, revert, review) easier.

Good commit messagesLesson 1 →

  • First line: short (about 50 characters), in the imperative: "Fix login redirect".
  • Blank line, then the why if it is not obvious. The diff already shows the what.
  • One idea per commit. If the message needs "and", it is probably two commits.
  • Put build output, secrets and editor files in .gitignore before the first add.

Branches and mergingLesson 2 →

git switch -c NAME
Create a branch and move to it
git switch NAME
Move to an existing branch
git branch -d NAME
Delete a merged branch
git merge NAME
Join NAME into the current branch
git merge --abort
Back out of a merge in trouble
Merge kindWhenResult
Fast-forwardTarget has no new commitsLabel just moves ahead
Merge commitBoth sides have new commitsA commit with two parents
ConflictBoth changed the same linesYou edit, add, then commit

Fixing a merge conflictLesson 2 →

  1. git status lists the files marked "both modified".
  2. Open each file and find the <<<<<<<, ======= and >>>>>>> markers.
  3. Keep the right lines, delete the three marker lines, and save.
  4. git add FILE, then git commit (or git merge --continue).
  5. Run the tests. Keep branches short-lived so conflicts stay small.

Remotes: fetch, pull, pushLesson 3 →

git remote -v
Show the remotes and their URLs
git fetch
Download new commits, change nothing locally
git pull
fetch plus a merge into your branch
git pull --rebase
fetch plus a rebase (straighter history)
git push -u origin NAME
Send a branch and remember it
git push
Send the current branch

Tip: When unsure, fetch first. It is always safe, and you can look before you merge.

Pull request flowLesson 3 →

  1. Branch from an up-to-date main, make small commits, push the branch.
  2. Open the pull request. Say what changed, why, how you tested it, and what to look at.
  3. Reviewers comment. Answer every comment, push fixes as new commits.
  4. Merge (merge commit, squash or rebase, as the team chose), then delete the branch.

Undo: which tool?Lesson 4 →

I want to…UseSafe on shared history?
Drop edits to a filegit restore FILEYes (local only)
Fix the last commitgit commit --amendOnly before pushing
Move the branch backgit reset --soft/--mixed/--hardOnly before pushing
Undo a pushed commitgit revert HASHYes: adds a new commit
Get a lost commit backgit reflog, then git switch -cYes
Park work for latergit stash / git stash popYes (local)

The three kinds of resetLesson 4 →

FlagBranch movesStaging areaFiles
--softYesKeptKept
--mixed (default)YesClearedKept
--hardYesClearedOverwritten

Watch out: --hard throws away uncommitted work. If you already committed it, git reflog can still find it.

Find and copy commitsLesson 4 →

git cherry-pick HASH
Copy one commit onto this branch
git bisect start
Begin a binary search for a bad commit
git bisect good / bad
Tell it how each checked commit behaves
git bisect reset
Finish and return to your branch
git reflog
Every place HEAD has been (your safety net)

Rebase and clean historyLesson 5 →

  • git rebase main replays your commits on top of the new main: a straight line, new hashes.
  • git rebase -i HEAD~N lets you squash, reword, drop or reorder your last N commits.
  • Rebase only commits nobody else has. Rewriting shared history breaks other people's copies.
  • After a rebase you must force push: use git push --force-with-lease, never plain --force.
git switch feature
git rebase main
# fix any conflicts, then:
git rebase --continue
git push --force-with-lease

Team rules on GitHubLesson 5 →

  • Branch protection on main: require a pull request, passing checks and a review.
  • CODEOWNERS names who must review which paths, so changes reach the right people.
  • GitHub Actions runs tests on every push: a file in .github/workflows/ with on, jobs, steps.
  • Pick one workflow (merge, squash or rebase) and write it down, so history stays consistent.

Interview one-linersLesson 0 →

QuestionShort answer
Merge vs rebase?Merge keeps true history; rebase rewrites it into a line. Never rebase shared commits.
fetch vs pull?pull is fetch plus merge (or rebase).
reset vs revert?reset moves the branch (local). revert adds an undoing commit (safe when shared).
What is HEAD?A pointer to the commit or branch you are on.
Lost a commit?git reflog lists where HEAD was; switch to that hash.

A cheat sheet is a reminder, not a teacher. Each card links to the lesson that explains it step by step.