Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine if two git branches will merge cleanly without affecting the working directory?

Tags:

git

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.

like image 862
Andrew Avatar asked May 15 '10 00:05

Andrew


People also ask

How do you check if there will be a merge conflict?

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 .

Does git merge affect both branches?

No, merging does only affect one branch.

What happens when you merge two git branches?

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.

How can you tell if two branches are identical?

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.


2 Answers

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.

like image 124
kralyk Avatar answered Sep 21 '22 19:09

kralyk


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.

like image 31
Cascabel Avatar answered Sep 21 '22 19:09

Cascabel