9

When I merge large changes, git often gets hopelessly confused because it's not using enough context lines. It gets mixed up between the similar-looking endings of two different subroutines that both happen to end with:

.
return 1;
.
}

(dots used here to represent blank lines)

When I use 'git diff', I can say -U20 to see 20 lines of context. But can I tell git to use this flag when it merges, too?

Could the answer be related to merge strategies/options, such as:

git merge -s recursive -X patience [branch]
Will Sheppard
  • 3,272
  • 2
  • 31
  • 41

2 Answers2

6

To the best of my knowledge, there's no way to increase the context lines during a merge. The patience diff algorithm is supposed to handle your use case better. The git-merge(1) man page describes it thus:

With this option, merge-recursive spends a little extra time to avoid mismerges that sometimes occur due to unimportant matching lines (e.g., braces from distinct functions). Use this when the branches to be merged have diverged wildly.

Since this looks exactly like the situation you're describing, patience seems custom-designed to help you out. You can try it out on a throw-away branch like so:

git checkout -b temp_merge_branch master
git merge --strategy-option=patience other_branch

If the merge works cleanly, you can fast-forward your master branch with the results. If not, you can just throw away the temporary branch, and look for other alternatives.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
6

Use these settings to get patience diffs with five lines of context everywhere:

git config --global diff.algorithm patience
git config --global diff.context 5

If you use --color-words often, this is useful as well:

git config --global diff.wordRegex '[A-z0-9_]+|[^[:space:]]'

The first setting requires Git 1.8.2.

Tobu
  • 24,771
  • 4
  • 91
  • 98