What differences are in the patches/files created by diff and git diff?

喜欢而已 提交于 2019-12-08 04:50:28

问题


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 diffand diff. So:

  1. On what does it depend whether you can use git diffand diff for the same file?
  2. And what are the differences between the formats?
  3. 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?
  4. If you can exchange the commands (see 1.): Is it even recommend to do so?
  5. Are there any other notable differences in the files created by the two commands?

回答1:


  1. On what does it depend whether you can use git diff and diff 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 use git diff in pretty much any situation you can use diff.
The reverse is not true

 git diff --color-words --no-index file1 file2

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


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


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


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!