问题
I wonder what differences are in the format of the files/patches created by diff
and git-diff
.
I know in diff
there are 3 (the "normal", the "compact" -c
one and the "unified" -u
one).
However there may be some differences and in some situations you cannot freely exchange git diff
and diff
. So:
- On what does it depend whether you can use
git diff
anddiff
for the same file? - And what are the differences between the formats?
- If you cannot exchange the commands (see 1.) how can you convert the files into the other format so that you can use them with the other command?
- If you can exchange the commands (see 1.): Is it even recommend to do so?
- Are there any other notable differences in the files created by the two commands?
回答1:
- On what does it depend whether you can use
git diff
anddiff
for the same file?
Simply if the file is in a git repo working tree, then you would be able to use git diff
to show changes for that file (against the same file as referenced by the git repo, like the index or blob objects).
This differs from 'diff', which compares files (meaning you need two files, not just one as in git diff when used in a git repo)
As hvd points out in the comments:
You can use git diff outside any work tree and pass it two files.
So you can usegit diff
in pretty much any situation you can usediff
.
The reverse is not true
git diff --color-words --no-index file1 file2
- And what are the differences between the formats?
git diff
can emulate any diff format (unified, raw, ...).
It has git-specific format as well (--summary
, --stat
, ...)
See also:
- Documentation/diff-format.txt
- "How to read the output from git diff?"
A git diff
will include a git header, with a "similarity index".
The hunks displays for each chunk of diffs are very similar to a diff -u.
- If you cannot exchange the commands (see 1.) how can you convert the files into the other format so that you can use them with the other command?
You can convert a git diff
in a raw format, or patch with raw: --patch-with-raw
.
The reverse is possible: you can apply a diff to a git repo.
- If you can exchange the commands (see 1.): Is it even recommend to do so?
It is if you don't have git installed (see the previous example)
- Are there any other notable differences in the files created by the two commands?
No: the result of applying a patch generated by a diff
or a git diff
should be the same.
来源:https://stackoverflow.com/questions/33713187/what-differences-are-in-the-patches-files-created-by-diff-and-git-diff