Git
Merging
Dry-run
Debugging
Version Control

Git merge dry run: how to preview a merge

Codemia Knowledge Hub ·

Git has no --dry-run option for git merge, unlike commands such as git clean or git add. You can still preview a merge: use git merge-tree to see the merged result without touching any branch, or run the real merge on a temporary branch.

A preview is especially useful in complex projects. It lets you:

  • Preview conflicts: find the files that would conflict.
  • Assess changes: see what would come in from the other branch.
  • Plan resolutions: decide how to handle problems before the actual merge.

Preview with git merge-tree

git merge-tree reads the tree objects of two branches and their merge base, and shows how the content would look after a merge without performing it. It is the closest thing to a merge dry run.

Syntax:

bash
git merge-tree $(git merge-base <branch1> <branch2>) <branch1> <branch2>

To preview merging feature and master:

bash
1# Find common ancestor of feature and master
2merge_base=$(git merge-base feature master)
3
4# Preview merge without making changes
5git merge-tree $merge_base feature master

Merge on a temporary branch

  1. Create a new branch from the target branch.
  2. Run the merge there.
  3. Review the result and any conflicts without affecting the real branches, then delete the temporary branch.
bash
1# Create and switch to a temporary branch
2git checkout -b temp-merge-branch master
3
4# Attempt to merge the feature branch into the temp branch
5git merge feature
6
7# Review changes and conflicts
8# If the merge stopped on conflicts, abort it first
9git merge --abort
10
11# Switch back and force-delete the temporary branch (-D, since its merge is not in master)
12git checkout master
13git branch -D temp-merge-branch

Comparing the two approaches

MethodCommandsProsCons
git merge-treegit merge-tree $(git merge-base feature master) feature masterDirect preview without affecting branchesOnly shows content changes, not actual merge
Temporary Branch Mergegit checkout -b temp-merge; git merge feature; git merge --abort; git checkout master; git branch -D temp-mergeFull merge simulation including conflict resolutionRequires branch management

Use git merge-tree for a quick, content-focused preview, and a temporary branch when you want a full trial that includes resolving conflicts.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions