Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In git, how do I increase the context for merges?

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]
like image 830
Will Sheppard Avatar asked Jun 28 '12 09:06

Will Sheppard


People also ask

What is the best git merge strategy?

The most commonly used strategies are Fast Forward Merge and Recursive Merge. In this most commonly used merge strategy, history is just one straight line. When you create a branch, make some commits in that branch, the time you're ready to merge, there is no new merge on the master.

Does git merge order matter?

However, Git always uses a single algorithm for computing both L and R, so the order does not matter here. To put this another way, if you manage to produce a Doppelgänger file—one that has different content from, but the same hash as, some existing file, Git simply refuses to put that file into the repository.


2 Answers

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.

like image 58
Todd A. Jacobs Avatar answered Oct 14 '22 15:10

Todd A. Jacobs


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.

like image 21
Tobu Avatar answered Oct 14 '22 14:10

Tobu