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.
git-mergetoolwill normally automatically stage files after the conflicts have been resolved. You will not need to manually rungit-add.However, in order to work with a variety of tools,
git-mergetoolwill 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>.trustExitCodein case this should not be trusted. If this is set tofalse, then git will always ask you if the merge was successful.In summary:
trustExitCodeoption 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.:
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.