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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJairView Question on Stackoverflow
Solution 1 - GitSandro MundaView Answer on Stackoverflow
Solution 2 - GitJojo ThomasView Answer on Stackoverflow