3

Git doesn't always choose hunk boundaries correctly, when calculating a changeset from snapshots. git diff has a --diff-algorithm option that allows some tweaking in this regard; git diff --minimal sometimes gives better results than git diff alone.

Is there a way to get the same optimised changeset layout also for git add -p (which basically shows the diff hunks interactively)? It doesn't seem to allow the --diff-algorithm option.

1
  • git add -p is (embedded within) a perl script (in $(git --exec-path)/git-add--interactive, so you could copy and edit it a bit. Hm, on further examination I see it inspects diff.algorithm from your configuration, so you can just set that. Commented May 29, 2016 at 12:28

1 Answer 1

7

TL;DR version

git -c diff.algorithm=minimal add -p 

(requires Git version 1.8.4 or newer).

Details

Moving this from comment to answer after the last bit of research:

Since Git version 1.8.4, the interactive add perl script (which implements git add -p) obeys the diff.algorithm setting in your git configuration. This went in with commit 2cc0f53 (12 Jun 2013) by John Keeping (johnkeeping).
(Merged by Junio C Hamano -- gitster -- in commit 91fc159, 27 Jun 2013) :

When staging hunks interactively it is sometimes useful to use an alternative diff algorithm which splits the changes into hunks in a more logical manner. This is not possible because the plumbing commands called by add--interactive ignore the "diff.algorithm" configuration option (as they should).

Since add--interactive is a porcelain command it should respect this configuration variable. To do this, make it read diff.algorithm and pass its value to the underlying diff-index and diff-files invocations.

At this point, do not add options to "git add", "git reset" or "git checkout" (all of which can call git-add--interactive). If a user wants to override the value on the command line they can use:

 git -c diff.algorithm=$ALGO ... 

(with a fix in commit e5c2909 (23 Jun 2013) by Junio C Hamano (gitster); the fix is also in 1.8.4).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.