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.
You can determine with git merge-base if a fast forward is possible, but is
Share
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 branchAandgit diff --name-only $merge_base branchBhave 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-workdirscript (from git.git’s contrib directory) can help with this; it creates a new repo whose.gitdirectory 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.