23

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.

2
  • 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... Commented Sep 14, 2012 at 14:35
  • see this line in the code : github.com/git/git/blob/master/builtin/add.c#L273 Commented Sep 14, 2012 at 16:21

3 Answers 3

39

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.

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

2 Comments

Sweet! Now I just have to wait for Cygwin Git to catch up…
Should it also change algorithm for git commit?
17

This worked for me:

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

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

3 Comments

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: 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.
@me_and the interesting thing here is that this config option is only temporary and can be passed as cli-flag this way
5

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.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.