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.
To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .
No, merging does only affect one branch.
Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.
In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.
I'd do this by creating a third, temporary branch. Let's say you want to merge branch branchFrom
into branchTo
. It would then go like this:
git checkout branchTo #only if not already at branchTo git checkout -b branchTmp git merge branchFrom # see what happens git checkout branchTo git branch -d branchTmp # act accordingly
That way you get accurate results without screwing any of your branches.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With