4.5.8 · HinglishSoftware Engineering

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

2,323 words11 min readRead in English

4.5.8 · Coding › Software Engineering


0. Mental model (pehle ye padho)

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

1. Branch — kaam ki parallel lines banana

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 — do histories ko jodna


3. Rebase — commits ko naye base pe replay karna

Andar se HOW kaam karta hai (derivation):

git switch feature
git rebase main
  1. feature aur main ka merge base dhundho.
  2. ke baad har feature commit ke diffs save karo.
  3. feature ko main ke tip pe move karo.
  4. Har diff ko order mein re-apply karo, naye commits banate hue.

4. Cherry-pick — ek commit kahin aur copy karna


5. Stash — uncommitted kaam shelve karna



Common mistakes (Steel-man + fix)


Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tumhara project history LEGO photos ki ek chain hai. Branch ek sticky-note hai jo ek photo pe label lagata hai. Committing = nayi photo lena aur apna sticky-note uspe move karna. Merge = do chains ko ek special "hum yahan mile" wali photo se glue karna. Rebase = apni recent photos ko peel off karna aur unhe newest chain ke upar phir se lena taaki ek seedhi line jaisi lage. Cherry-pick = sirf ek achhi photo apne album mein copy karna. Stash = apna adha-bana LEGO ek box mein rakhna taaki table clean ho. Bisect = ek guessing game: "kya LEGO aadhe mein toota hai? haan? to break pehle aadhe mein hai" — tab tak aadha karte raho jab tak exact bura brick na mil jaye.


Flashcards

Git branch physically kya hai?
Ek movable pointer (.git/refs/heads/ mein ek chhoti file) jisme ek commit ka SHA hota hai.
HEAD kya point karta hai?
Tumhari current location — usually ek branch (jo badle mein ek commit ko point karti hai).
Git fast-forward merge kab karta hai?
Jab target branch source ki direct ancestor ho, to ye sirf pointer ko aage slide karta hai bina merge commit ke.
Ek (three-way) merge commit ke kitne parents hote hain?
Do.
Merge base kya hota hai?
Merge ho rahi do branches ka most recent common ancestor.
Merge conflict exactly kab hota hai (per region)?
Jab dono sides ne base ke relative ek hi region ko alag-alag change kiya ho (, , ).
Rebase commit SHAs ke saath kya karta hai?
Ye brand-new commits banata hai naye SHAs ke saath (history rewrite hoti hai).
Rebasing ka golden rule batao.
Un commits ko kabhi rebase mat karo jo already push/share ho chuke hain.
Merge aur rebase ke outcome mein kya fark hai?
Merge ek merge commit record karta hai (sachi, branched history); rebase replayed commits ki linear history produce karta hai.
Cherry-pick kya apply karta hai?
Ek single commit ka diff (parent→commit) current branch pe naye commit ke roop mein re-apply kiya jaata hai.
Cherry-pick karne ke baad usi branch ko merge karne ka risk kya hai?
Change do baar appear hoti hai (duplicate commits) → potential conflicts.
Git stash kya karta hai?
Dirty working tree + index ko ek stack pe save karta hai aur clean HEAD pe revert karta hai.
Stash pop aur stash apply mein kya fark hai?
pop = apply phir stack se remove karo; apply entry stack pe rakhta hai.
Git bisect kaunsa algorithm use karta hai?
Commit history par binary search.
Bisect ko N commits ke liye kitne tests chahiye?
Lagbhag ⌈log₂ N⌉.
Bisect session abandon karne ka command kya hai?
git bisect reset.
FF possible hone ke bawajood merge commit force kaise karo?
git merge --no-ff.

Connections

  • Directed Acyclic Graph — commit history ki underlying data structure
  • Binary Searchgit bisect ke andar ka engine
  • Three-way merge algorithm
  • Trunk-based vs GitFlow — in operations pe bane workflows
  • Conflict resolution aur Diff and patch (Myers algorithm)
  • Immutability and content-addressable storage — commits mein SHAs kyun hote hain

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