4.5.8Software Engineering

Git operations — branch, merge, rebase, cherry-pick, stash, bisect

2,431 words11 min readdifficulty · medium

0. The mental model (read this first)

Figure — Git operations — branch, merge, rebase, cherry-pick, stash, bisect

1. Branch — creating parallel lines of work

HOW (commands):

git branch feature        # create pointer, stay on current branch
git switch feature         # move HEAD to feature  (old: git checkout feature)
git switch -c feature       # create + switch in one go

2. Merge — joining two histories


3. Rebase — replaying commits onto a new base

HOW it works internally (derivation):

git switch feature
git rebase main
  1. Find merge base MM of feature and main.
  2. Save the diffs of each feature commit C1,,CnC_1,\dots,C_n after MM.
  3. Move feature to main's tip.
  4. Re-apply each diff in order, creating new commits C1,,CnC_1',\dots,C_n'.

4. Cherry-pick — copy one commit elsewhere


5. Stash — shelve uncommitted work


6. Bisect — binary search for the bad commit


Common mistakes (Steel-man + fix)


Recall Feynman: explain to a 12-year-old

Imagine your project history is a chain of LEGO photos. A branch is a sticky-note labeling one photo. Committing = taking a new photo and moving your sticky-note to it. Merge = gluing two chains together with a special "we joined here" photo. Rebase = peeling off your recent photos and re-taking them on top of the newest chain so it looks like one straight line. Cherry-pick = copying just one cool photo into your album. Stash = putting your half-built LEGO in a box so the table is clean. Bisect = a guessing game: "is the LEGO broken halfway? yes? then the break is in the first half" — keep halving until you find the exact bad brick.


Flashcards

What is a Git branch, physically?
A movable pointer (small file in .git/refs/heads/) containing the SHA of a commit.
What does HEAD point to?
Your current location — usually a branch (which in turn points to a commit).
When does Git do a fast-forward merge?
When the target branch is a direct ancestor of the source, so it just slides the pointer forward with no merge commit.
How many parents does a (three-way) merge commit have?
Two.
What is the merge base?
The most recent common ancestor of the two branches being merged.
When exactly does a merge conflict occur (per region)?
When both sides changed the same region differently relative to the base (OBO\neq B, TBT\neq B, OTO\neq T).
What does rebase do to commit SHAs?
It creates brand-new commits with new SHAs (history is rewritten).
State the golden rule of rebasing.
Never rebase commits that have already been pushed/shared.
Difference between merge and rebase outcome?
Merge records a merge commit (true, branched history); rebase produces a linear history of replayed commits.
What does cherry-pick apply?
The diff of a single commit (parent→commit) re-applied as a new commit on the current branch.
Risk of cherry-picking then merging the same branch?
The change appears twice (duplicate commits) → potential conflicts.
What does git stash do?
Saves dirty working tree + index to a stack and reverts to a clean HEAD.
Difference between stash pop and stash apply?
pop = apply then remove from stack; apply leaves the entry on the stack.
What algorithm does git bisect use?
Binary search over commit history.
How many tests does bisect need for N commits?
About ⌈log₂ N⌉.
Command to abandon a bisect session?
git bisect reset.
How do you force a merge commit even when FF is possible?
git merge --no-ff.

Connections

  • Directed Acyclic Graph — the underlying data structure of commit history
  • Binary Search — the engine inside git bisect
  • Three-way merge algorithm
  • Trunk-based vs GitFlow — workflows built on these operations
  • Conflict resolution and Diff and patch (Myers algorithm)
  • Immutability and content-addressable storage — why commits have SHAs

Concept Map

built from

points to

movable pointer to

points to

advanced by new commit on

creates commit with

two parents preserve

both sides differ causes

if ancestor becomes

replays commits onto

copies single

DAG of commits

Commit snapshot

Branch pointer

HEAD

Merge

Merge base

Conflict

Fast-forward

Rebase

Cherry-pick

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Git ko samajhne ka sabse aasaan tareeka: socho ki tumhari poori project history ek chain of photos (commits) hai, aur har photo apne pichle photo (parent) ko point karta hai. Ek branch sirf ek sticky-note hai jo kisi ek photo pe laga hai — isliye branch banana O(1) hota hai, bas 41 bytes likhna. Jab tum commit karte ho, to naya photo banta hai aur tumhara sticky-note aage khisak jaata hai.

Merge ka matlab hai do chains ko jodna ek special "yahan mile the" photo (merge commit, do parents) ke saath. Agar koi divergence nahi hai to Git sirf pointer aage slide kar deta hai — isko fast-forward kehte hain, koi naya commit nahi banta. Rebase thoda alag hai: ye tumhare recent commits ko utha ke latest base ke upar dobara apply karta hai, history seedhi (linear) dikhne lagti hai — lekin SHA badal jaate hain. Isliye Golden Rule yaad rakho: jo commit push ho chuka hai use kabhi rebase mat karo, warna teammates ki history toot jaayegi.

Cherry-pick ka use tab karo jab kisi dusri branch ka sirf ek hi fix chahiye — wo us ek commit ka diff utha ke yahan naya commit bana deta hai. Stash tumhara aadha kaam ek box mein rakh deta hai taaki table saaf ho jaaye aur tum branch switch kar sako, baad mein stash pop se wapas le aao. Aur bisect ek guessing game hai — "good" aur "bad" commit batao, Git beech wala check karta hai, har baar aadha hissa kaat deta hai, to 1000 commits mein bhi sirf ~10 tests mein bug pakad leta hai (binary search, log2N\log_2 N). Bas yahi 6 cheezein roz ke development mein 80% kaam cover karti hain.

Go deeper — visual, from zero

Test yourself — Software Engineering

Connections