tags:

views:

44

answers:

2

You can determine with git merge-base if a fast forward is possible, but is there some git trick to determine if two branches will merge cleanly with some strategy without actually doing the merge? I know about "git merge --no-commit --no-ff $BRANCH" but that affects the working directory, which I'd like to avoid since this is part of a webservice.

+2  A: 

There's no built-in way; a work tree is required to perform a merge. Seeing if a merge will work (in the general case) means trying the strategy and seeing what happens.

You could however check for the trivial case: the two branches don't touch the same files. Find the merge base, and then check if git diff --name-only $merge_base branchA and git diff --name-only $merge_base branchB have anything in common.

Otherwise, you'll need a work tree to try the merge in. You could easily create a second one - either clone the repository, or to save space, just create a work tree. The git-new-workdir script (from git.git's contrib directory) can help with this; it creates a new repo whose .git directory is full of symlinks back to the original one. Just be careful that in that new work directory you don't modify the branch the original repo has checked out - they'll get out of sync, in the same way that pushing into a currently checked out branch messes things up.

Jefromi
A: 

You can't do it without affecting the working directory, but you can do it without affecting your current changes.

git stash

git merge ...

woops

git reset --hard HEAD

git stash apply

Will get you what you need.

iwein