How can I know if a branch has been already merged into master?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To know whether a branch has already been merged into master, you need to ask Git whether the branch tip is reachable from master. The simplest checks are built into git branch --merged and git merge-base. The main complication is not the command itself but the history style: rebases, squash merges, and stale remote refs can make "merged" mean slightly different things operationally.
Start with git branch --merged
If you want a quick local answer, switch to master and ask Git which branches are fully merged into the current branch:
If your feature branch appears in that list, its tip is reachable from master, which means Git considers it merged.
You can also ask directly:
This is the most convenient human-facing command for normal merge history.
Use merge-base --is-ancestor for a Scriptable Check
If you want a precise yes or no result, use:
Interpretation:
- '
0meansfeature-branchis an ancestor ofmaster' - non-zero means it is not
That makes this command especially useful in scripts or CI checks.
It is effectively asking: "Is every commit reachable from the feature branch tip already contained in master?"
Fetch First So You Are Asking About Current History
Before checking remote branches, update your refs:
Then inspect the remote branch against remote master:
Without a fresh fetch, you may be checking outdated local knowledge of the remote repository.
This is one of the most common sources of confusion. The command is correct, but the refs are stale.
Know the Difference Between Merge, Rebase, and Squash
These checks work cleanly when the branch was merged normally. But branch history style matters:
- normal merge commit: ancestor checks work well
- fast-forward merge: ancestor checks also work well
- rebase then merge: still usually fine if the rebased commits are what landed
- squash merge: the original branch tip is often not an ancestor anymore
In a squash-merge workflow, the changes may be present in master even though the original branch tip is not. In that case, Git may correctly report that the branch is not merged from a commit-graph perspective.
That is why "has it been merged?" can mean two different things:
- is the branch tip reachable from
master - are the branch changes present in
masterin some equivalent form
Git answers the first question directly. The second question is more semantic and harder to automate.
Use Log Comparisons When the Workflow Is Less Direct
If the history is unusual, inspect what commits are still unique to the branch:
If nothing prints, there are no commits on the branch that are absent from master under normal ancestry rules.
You can also compare the reverse direction:
These views help when the raw yes or no answer is not enough.
Common Pitfalls
- Checking local branch state without fetching updated remote refs first.
- Assuming squash merges preserve branch ancestry in the same way as normal merges.
- Treating "changes are present" and "branch tip is merged" as if they were always identical questions.
- Running
git branch --mergedfrom the wrong current branch and misreading the result. - Deleting a branch based on stale assumptions instead of verifying against the relevant target branch explicitly.
Summary
- '
git branch --merged masteris the easiest quick check for normal merge history.' - '
git merge-base --is-ancestor branch masteris the precise script-friendly test.' - Fetch remote refs before checking remote merge status.
- Squash merges can make branch ancestry checks look negative even when similar changes landed.
- Be clear whether you care about branch-tip ancestry or about the presence of equivalent changes in the target branch.

