Finding a branch point with Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A branch point (or fork point) is the commit where a branch diverged from its parent branch. Git does not explicitly store this information — branches are just pointers to commits, and any commit can be an ancestor of multiple branches. To find the branch point, you use git merge-base to find the common ancestor between two branches. This is essential for understanding what changed on a branch, preparing for merges, or creating targeted rebases.
Method 1: git merge-base (Most Common)
git merge-base finds the best common ancestor — the most recent commit that is reachable from both branches. This is the branch point.
Method 2: git merge-base --fork-point (Reflog-Aware)
--fork-point consults the reflog to handle cases where main was rebased or force-pushed after the branch was created. It finds the original fork point even if the common ancestor changed.
Method 3: Visual with git log --graph
Method 4: git log with Range
Method 5: git diff from Branch Point
The three-dot ... syntax in git diff automatically uses the merge base, showing only what changed on the right side since divergence.
Method 6: Find Branch Point in a Script
Multiple Merge Bases
When branches have been merged back and forth, there can be multiple common ancestors:
Practical Use Cases
Rebase onto the correct point
Create a patch of all branch changes
Count commits on a branch
Common Pitfalls
- Assuming Git stores the branch point explicitly: Git does not record where a branch was created. It only stores commit parents.
git merge-basecomputes the branch point from the commit graph each time. If the graph is complex (many merges), the result may not match your intuition. - Reflog expiration with
--fork-point:--fork-pointdepends on the reflog, which expires after 90 days by default. On CI servers or fresh clones, the reflog may be empty, causing--fork-pointto return nothing. Use plaingit merge-baseas a fallback. - Shallow clones:
git clone --depth Ncreates a shallow clone with limited history.git merge-basemay fail or return incorrect results because the branch point commit is not in the local repository. Usegit fetch --unshallowto get full history. - Confusing
..and...ingit logvsgit diff: Ingit log,A..Bshows commits in B not in A. Ingit diff,A...Buses the merge base (branch point). The three-dot syntax has different meanings depending on the command. - Multiple merge bases after criss-cross merges: If branches have been merged back and forth,
git merge-basereturns the most recent common ancestor, which might not be the original fork point. Usegit merge-base --allto see all candidates.
Summary
- Use
git merge-base feature mainto find the commit where a branch diverged - Use
--fork-pointfor reflog-aware detection (handles rebased base branches) - Use
git diff main...feature(three dots) to see changes since the branch point - Use
git log main..feature(two dots) to list commits on the feature branch - Git does not store branch points explicitly — they are computed from the commit graph
- Shallow clones and expired reflogs can cause incorrect branch point detection
Related reading
- Finding diff between current and last version
- Finding diff between current and last version
- Finding kth smallest number from n sorted arrays
- Finding what branch a Git commit came from
- Finding what branch a Git commit came from
- Flutter CocoaPods's specs repository is too out-of-date to satisfy dependencies
- For an Amazon S3 bucket deployment from GitHub how do I fix the error AccessControlListNotSupported The bucket does not allow ACLs?
- Force add despite the .gitignore file
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.