21

I'd like to use Git's patience diff algorithm (the one you get if you invoke git diff with the --patience argument) with git add -p. How can I do this?

Background: I'm working with some XML files, and git diff's normal algorithm produces pretty poor diffs due to "misaligned" entry/exit tags. If I run git diff --patience, I get much more useful diffs, but there's no obvious way to use these diffs in git add -p.

Community
  • 1
  • 1
me_and
  • 13,911
  • 6
  • 58
  • 93
  • 3
    I'm not sure you can do that right now, but it sounds like a worthy suggestion to make to the `git` developers... In fact, there may be other `git diff` options that it would be useful to expose to `git add` and other places where an otherwise default-ish `git diff` is done... – twalberg Sep 14 '12 at 14:35
  • see this line in the code : https://github.com/git/git/blob/master/builtin/add.c#L273 – greg0ire Sep 14 '12 at 16:21

3 Answers3

36

git add -p currently rejects diff flags, but you can use the diff.algorithm config option:

git config --global diff.algorithm patience

New in Git 1.8.2.

Tobu
  • 23,001
  • 3
  • 85
  • 97
14

This worked for me:

git -c diff.algorithm=patience add -p [...]

I am running the latest git v2.1.0 in Cygwin.

Amro
  • 121,265
  • 25
  • 232
  • 431
  • 3
    Yes, as @Tobu already noted in the accepted answer, that config flag was added in Git v1.8.2. It turns out the way to get an up-to-date Git build on Cygwin is to take over the maintainership, which is exactly what I ended up doing :) – me_and Sep 10 '14 at 23:59
  • @me_and: then I have you to thank for finally getting updated versions of Cygwin git :) It's been inactive for quite some time.. As for my answer, I just wanted to show it's possible to change a config inline without modifying global configurations. – Amro Sep 11 '14 at 00:28
  • 1
    @me_and the interesting thing here is that this config option is only temporary and can be passed as cli-flag this way – ohcibi Aug 12 '15 at 20:03
4

Hmmmm... One thing you could do is pipe the output of git diff to a temporary place, then read it back in with git apply:

git diff --patience <commitA> <commitB> > /tmp/patch.out
# checkout a new branch or otherwise do what you need to prep
git apply < /tmp/patch.out

That'll apply the output of the diff to the working directory, but it won't commit or stage the changes. You can then run git add -p as you normally would, and the --patience diff will be the changes you're interactively adding.

There's no reason you couldn't pipe diff to apply directly, if it better suits your workflow. It's something I do fairly regularly when rebuilding my own local branches for production integration.

Looks like you can also use it as a merge strategy, so it might be the case that instead of interactively adding the diffs, you could simply create a branch with what you want, then merge it in.

Community
  • 1
  • 1
Christopher
  • 36,834
  • 9
  • 72
  • 91
  • That's what I'm currently doing; it lets me edit the patch, which is essentially what I'd like to do with `git add -p`. This just seems sadly more tedious than it ought to be. – me_and Sep 14 '12 at 13:47
  • @me_and Added some notes about using it as a merge strategy, too. Perhaps you could generate a trash merge branch with just the changes you wanted, then use the merge strategy to get a better result. – Christopher Sep 14 '12 at 13:48