How to check the conflict of two branch, but not need to merge them?

I can't merge two branch because of release step of our project, but I want to know whether there are conflict of two branch. How can I do this?


Solution 1:

Suppose you are on the master branch and you would like to test if the dev branch can be merged without conflict into the master.

# In the master branch
git merge dev --no-ff --no-commit

After that, you will be able to know if there's a conflict or not.

To return in a normal situation, just abort the merge:

git merge --abort

According to the git documentation:

--ff
Do not generate a merge commit if the merge resolved as a fast-forward, only update the branch pointer. This is the default behavior.

-no-ff
Generate a merge commit even if the merge resolved as a fast-forward.

--commit
Perform the merge and commit the result. This option can be used to override --no-commit.

--no-commit
With --no-commit perform the merge but pretend the merge failed and do not autocommit, to give the user a chance to inspect and further tweak the merge result before committing.

Solution 2:

Providing an answer based on a use case - you are on a feature branch - working on a change. Meanwhile another colleague makes changes on a shared file and merges into trunk. Now you are ready to merge. At this point you will have the issue of merge conflict.

Ensure you have latest version of trunk branch

git fetch
git checkout trunk
git pull

Switch back to feature branch

git checkout feature

In the feature branch, simulate merge into trunk branch as a fast forward (--no-ff). The --no-commit will prevent a autocommit

git merge trunk --no-ff --no-commit

Inspect the files.

  • The lines between [<<<<<<< HEAD] and [=======] are the ones in feature branch
  • The lines between [=======] and [>>>>>>> trunk] are the ones in trunk branch

To abort the simulated merge and go back to your original state, run

git merge --abort