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:
To preview merging feature and master:
Merge on a temporary branch
- Create a new branch from the target branch.
- Run the merge there.
- Review the result and any conflicts without affecting the real branches, then delete the temporary branch.
Comparing the two approaches
| Method | Commands | Pros | Cons |
git merge-tree | git merge-tree $(git merge-base feature master) feature master | Direct preview without affecting branches | Only shows content changes, not actual merge |
| Temporary Branch Merge | git checkout -b temp-merge; git merge feature; git merge --abort; git checkout master; git branch -D temp-merge | Full merge simulation including conflict resolution | Requires 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.
Related reading
- Is there a good visual Git tool for Mac OS X or Windows?
- Is there a naming convention for git repositories?
- Is there a standard naming convention for git tags?
- Is there a "theirs" version of "git merge -s ours"?
- Is there a 'max-retries' for Kubernetes Jobs?
- is there a mongoose connect error callback
- Is there a way to autosign commits in Git with a GPG key?
- Is there a way to cache https credentials for pushing commits?
.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 courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.