Git inside

A commit is a snapshot that points at the one before; a branch is just a label on a commit. Watch merge, rebase and squash bring the same branch into main three ways, then undo a commit with revert and with reset.

Level: IntermediateWatch: 12 steps · ~1 minuteRun git commands on a graphRecall: 3 questions · 8 cardsCompare merge · rebase · squashgit · shell

What you'll take away

  • Explain what a commit and a branch really are: a snapshot, and a label on one
  • Pick merge, rebase or squash, and know which one rewrites commits
  • Undo a pushed commit with git revert, and keep reset --hard for your own work
  • Get a lost commit back from git reflog after a reset or a rebase

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
git commitmain → A
main
git commit → …
Step 1 / 12 · step by step
1Commits on main
0merge commit
0Rewritten
yesStraight history
Amain points at

The picture: each building is a commit, a snapshot of the whole project, with its name on its front and the change it made as a tile on its roof (blue: made on main, violet: made on feature). Each road runs from a commit back to its parent: main's street at the back, feature's in front. The labels are branches, with a ring on the ground round the commit each one points at. A faded, dashed building is a commit no branch points at any more; git still keeps it for a while.

Step 4 · Every option at once, on one clock

Compare them all: Git inside

The same graph in every tile: main has A B C, feature split at B with D E. Bring feature into main three ways, and see the history each leaves, which commits are rewritten, and how many end up on main.

0.0 s
Mergegit merge featuremain 6 commit · merge 1 · rewritten 0

Use it when Bringing in a branch others use too: no existing commit is touched, and the real history shows where it split and joined

Skip it when Wanting a straight, easy-to-read history: lots of merge commits tangle the graph

Commit names in the picture are labels, not real hashes; D′ is a new commit with D's changes. git.sh below shows real ones. Press any tile to see it large above, with when it fits.

What each keeps

CommandHistoryRewrites commitsOn a shared branch
mergeKept, with a merge commitNoSafe
rebaseA straight lineYes: new hashesDon't
squash mergeOne commit on mainThe branch's steps don't reach mainSafe
revertKept, plus an undo commitNoSafe
reset --hardThe branch moves backLeaves commits behindDon't

Nothing is edited, only added

A commit never changes: its hash is computed from its content, its parents, the author and the time. "Rewriting history" really means making new commits and moving labels to them. That's why a rebase on a shared branch hurts: everyone else still holds the old ones.

Lost something? Try the reflog

git reflog lists where HEAD has been. A commit left behind by a reset or a rebase is usually still there for a while; make a branch from its hash and it's back.

In real code

git.sh
#!/bin/sh
# Merge, rebase and squash on the same two branches, then revert and reset, in throwaway repositories.
# Fixed names and dates, so the hashes come out the same every run (a commit's hash covers its content, parents,
# author and time). Run: sh git.sh   (needs git 2.28+)
set -eu
export GIT_AUTHOR_NAME=Mai GIT_AUTHOR_EMAIL=mai@example.com GIT_COMMITTER_NAME=Mai GIT_COMMITTER_EMAIL=mai@example.com
export GIT_AUTHOR_DATE="2026-11-25T10:00:00+0700" GIT_COMMITTER_DATE="2026-11-25T10:00:00+0700"
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

start() { # main: A B C; feature, from B: D E
  git init -q -b main "$tmp/$1" && cd "$tmp/$1"
  for c in A B; do echo "$c" > "$c.txt" && git add . && git commit -qm "$c"; done
  git switch -qc feature
  for c in D E; do echo "$c" > "$c.txt" && git add . && git commit -qm "$c"; done
  git switch -q main && echo C > C.txt && git add . && git commit -qm C
}

start merge
git merge -q --no-edit feature
echo "merge:  $(git rev-list --count main) commits on main, $(git rev-list --merges --count main) merge commit"

start rebase
d=$(git rev-parse --short feature~1)
git switch -q feature && git rebase -q main && git switch -q main && git merge -q --ff-only feature
echo "rebase: $(git rev-list --count main) commits on main, $(git rev-list --merges --count main) merge commits, D was $d and is now $(git rev-parse --short main~1)"

start squash
git merge -q --squash feature > /dev/null 2>&1 && git commit -qm "feature: D and E"
echo "squash: $(git rev-list --count main) commits on main, D and E as one"

start undo
git revert --no-edit HEAD > /dev/null
echo "revert: $(git rev-list --count main) commits, C kept in the history: $(git log --format=%s | grep -cx C)"
git reset -q --hard HEAD~2
echo "reset --hard HEAD~2: main at $(git log -1 --format=%s), $(git rev-list --count main) commits, C still in the reflog: $(git reflog --format=%gs | grep -c 'commit: C')"
Run: sh git.sh (needs Docker)It printed:
merge:  6 commits on main, 1 merge commit
rebase: 5 commits on main, 0 merge commits, D was 9277dfc and is now 9344968
squash: 4 commits on main, D and E as one
revert: 4 commits, C kept in the history: 1
reset --hard HEAD~2: main at B, 2 commits, C still in the reflog: 1

Every command in the lesson, run in throwaway repositories: merge, rebase (D gets a new hash), squash, then revert and reset --hard (C still in the reflog). Fixed names and dates make the hashes the same on every run. It really runs with git; below is what it printed.

Common questions

What is a git branch, really?

A small label, a file holding one commit's hash, that moves along each time you commit on it. That's why making or deleting a branch takes no time at all. The real thing is the commits, each pointing back at its parents.

Merge vs rebase: what's the difference?

Merge makes a new commit with two parents and leaves every existing commit alone, so the history shows where things split and joined. Rebase replays your commits after the other branch: a straight, easy line, but every replayed commit is new, with a new hash.

Why shouldn't I rebase a branch others have pulled?

Rebase doesn't edit commits, it makes new ones (D becomes D′). You'd have to force push, and anyone holding the old ones now has a history that doesn't match yours. The rule: rebase only your own branch that nobody has pulled yet.

Table: what each keeps

How is a squash merge different from a normal merge?

git merge --squash puts all of feature's changes into one commit on main. A clean history, handy for a pull request full of small commits, but the small steps never reach main and git doesn't know feature has been merged.

A commit I pushed to main broke things. How do I undo it?

git revert <commit>: a new commit that does the opposite, nothing removed, and everyone pulls as usual. Don't reset --hard and force push a shared branch: everyone else still has the commits you threw away, and the histories stop matching.

What's the difference between reset --soft, --mixed and --hard?

All three move the branch back to the commit you name. --soft keeps the changes staged, --mixed (the default) keeps them in your files but unstaged, and --hard throws away the changes in your files too.

I ran reset --hard by mistake. Can I get it back?

Usually, if it wasn't too long ago. git reflog lists where HEAD has been: find the old hash, make a branch from it, and it's back. Changes you never committed are another story; the reflog never had them.

Read: lost something? Try the reflog

Why does a rebased commit get a new hash?

A commit's hash is computed from its content, its parents, the author and the time. Replay it somewhere else and its parent changes, so its hash does too. A commit is never edited: "rewriting history" means making new commits and moving the labels to them.

Next lesson

Git securityLevel: Intermediate

History keeps everything, a token you deleted included; next, rotate leaked secrets first and protect the branches others share.