Question

I'm unclear on the correct sequence of Git commands required to complete a merge that has resulted in conflicts. I understand that one approach is to follow the rejected merge with something like

git mergetool
# Then once everything has been fixed
git add ??
git commit -m "Some message"

but it isn't clear to me exactly what the git-add command should consist of in order to ensure that only the files I originally staged for the rejected merge are ultimately committed, and that only those files are committed.

Was it helpful?

Solution

git-mergetool will normally automatically stage files after the conflicts have been resolved. You will not need to manually run git-add.

However, in order to work with a variety of tools, git-mergetool will try to verify that the file has been updated (see check_unchanged()) by checking the modification date. If the file has been modified, then, it will check the return code from the mergetool.

Some mergetools may not return the correct exit code (0 if the merge 'succeeded'), and so git also provides a configuration option mergetool.<tool>.trustExitCode in case this should not be trusted. If this is set to false, then git will always ask you if the merge was successful.

In summary:

  1. Your mergetool should save the file to disk after merging, even if no changes are necessary (which shouldn't be possible, since it wouldn't be a merge conflict).
  2. If you don't trust the exit code of your mergetool, you can set the trustExitCode option to false.

In practice, I recommend using one of the merge utilities that git now natively supports. You can get a list of these here. If you use one of these tools, you should not need any additional setup beyond configuring git to use it, e.g.:

git config --global merge.tool p4merge

Note: You may find a lot of outdated articles around the web about setup for these tools. My favorite mergetool, p4merge, for example, was not natively supported by git at first, and required some manual configuration. This is no longer necessary for recent versions of git.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top