Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if git merge problems have been fixed?

Tags:

git

git-merge

When merging branches it is common to get merge conflicts which are listed on the command prompt. I know how to get the issues fixed by looking at the files that have conflicts and making the appropriate changes. We also have "mergetool" which can be very useful (not very good at using it though). From what I've read from different sources the steps to get this resolved are:

  • fix conflicts
  • add
  • push

This seems pretty simple. But what if I miss something and don't fix it? Does Git provide a way to check if there is something else to be fixed other than using mergetool?

like image 203
polaris Avatar asked Dec 20 '16 15:12

polaris


People also ask

How do you check if all merge conflicts are resolved?

If you are able to complete the merge the conflicts must have been resolved. Once you add and commit a merge, you're telling Git that there are no more changes you need to make. If you miss something, then that's your problem. Fix it and recommit or --amend your merge commit.

How are merge conflicts detected?

Merge conflicts occur when you attempt to merge one set of changes with another, conflicting set of changes. Git uses a simple three-way algorithm to detect merge conflicts. If Git detects a conflict when merging, it halts the merge and asks for manual intervention to resolve the conflict. git config merge.


2 Answers

Do:

git diff -S "<<<<<<< HEAD" -S "=======" -S ">>>>>>> $(git name-rev --name-only MERGE_HEAD)" HEAD

This compares the contents of the worktree with HEAD, but only shows any output if one or more of the three types of merge marks is included in the change.

For example, if you are merging from a branch called develop, an unmerged file might look like this:

public void fooTheBar(Bar input) {
<<<<<<< HEAD
  if (input == null) {
    throw new Exception("input cannot be null!");
  }
=======
  Console.WriteLine("preparing to foo the bar");
>>>>>>> develop
  input.foo();
}

So to ensure all files have been merged, you need to search for any of the following three lines:

<<<<<<< HEAD
=======
>>>>>>> develop

And that is what the -S arguments in the command do. Since it won't always be develop, we use the command:

git name-rev --name-only MERGE_HEAD

to get the name of the branch that you are merging into your current branch.

(You could probably search for just one of those lines, but searching for all three is more robust, and will show you cases where e.g. you forgot to remove just one of the lines.)

Since the command compares the worktree to HEAD, and not just to the staged changes, this will work even if you have git added a file that still contains conflicts.

like image 157
Scott Weldon Avatar answered Sep 24 '22 03:09

Scott Weldon


Of course:

git merge --continue

will choke if you have still conflicts or have fixed conflicts without having add or rm the files yet.

For fresh merges:

git grep -En '<{7} HEAD'

A custom command is even more flexible. The following example will ignore binary files:

find -exec grep -EIHn --color=auto '<{7}' {} \;

Or limit the search to C++ files:

find \( -name '*.cpp' -or -name '*.h' \) -exec grep -EHn --color=auto '<{7}' {} \;

To turn such a script into a real git command save as git-conflicts in the path. Now you should be able to use:

git conflicts

An advantage of a custom command is the option to make git-conflicts repository-aware, specialize the set of files further or even find "TODO" or "BUG" tags etc.

Git custom commands

like image 28
Andreas Spindler Avatar answered Sep 22 '22 03:09

Andreas Spindler