9

I have always used diff -b to make patches even when working on a git repo.

  1. What is the difference between the two, does git diff / git format-patch also internally makes use of the linux/unix diff command? (I know the difference between git diff/git format-patch)
  2. Also, how is patching with patch -p1 different from git apply. Can i apply a patch generated by the diff command (diff -b) and apply using git diff ?
  3. Is it a good practice to use git diff/apply when working with git? I have been using diff/patch and never faced any problem.

Please correct me if my knowledge of things mentioned above is not adequate.

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • cross site dupe https://unix.stackexchange.com/questions/356652/is-git-diff-related-to-diff/598031#598031 also see my answer https://unix.stackexchange.com/a/598031/171025 – qwr Jul 12 '20 at 06:22

1 Answers1

9

In order:

  1. Git has its own built-in diff but the outputs of both are quite similar, given the right options. Using the built-in diff and the git diff front-end gets you a whole lot of automation, plus the output is always something git apply or git am will like. In short, it's just a lot more convenient.

  2. I've done this sort of thing (fed git diff output to patch, or plain diff output to git apply). It works, although occasionally I have had to edit things here and there to get it to work, which is a pain. It's much more convenient to just get a git diff if I want to git apply or git am a patch. The biggest observable difference in general is that git apply does not do partial apply by default: you must add --reject to make it act like patch's default. (Also, all those .orig files...)

  3. Yes, because of said convenience. When you go a bit further and use git format-patch and git am, you can mass-apply a whole series of patches, maintaining commit messages including authorship information and so on, and getting everything committed automatically.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks torek! nicely put. – brokenfoot Mar 13 '14 at 19:26
  • 2
    Incidentally, I've done the mix-n-match thing: instead of `git apply --reject`, just use `patch` to apply. Sometimes the high powered electric screwdriver just seems like overkill, and I use the hand one. :-) – torek Mar 13 '14 at 19:29