git diff 的 /a /b 前缀的原因是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6764953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the reason for the /a /b prefixes of git diff
提问by mkl
I've been using git for some years now and always wondered why git diff prefixes the names of modified files with a/ and b/. I expected to eventually stumble upon a use-case where it is useful, but until now it was always annoying and never helpful.
我已经使用 git 好几年了,总是想知道为什么 git diff 用 a/ 和 b/ 前缀修改文件的名称。我希望最终会偶然发现一个有用的用例,但直到现在它总是很烦人而且从来没有帮助。
What is it good for? Why is this enabled by default? In which situations is it useful?
到底有什么好处呢?为什么默认启用?在哪些情况下有用?
回答by VonC
As mentioned in the diff man page, a/ et b/ represent the prefix to differentiate source and destination.
如diff 手册页所述,a/ et b/ 代表区分源和目标的前缀。
Actually, you have the options:
实际上,您有以下选择:
--no-prefix
Do not show any source or destination prefix.
不要显示任何源或目标前缀。
--src-prefix=<prefix>
Show the given source prefix instead of "a/".
显示给定的源前缀而不是“a/”。
--dst-prefix=<prefix>
Show the given destination prefix instead of "b/"
显示给定的目的地前缀而不是“b/”
回答by Will Sheppard
If you don't find it useful, you can turn it off with:
如果你觉得它没有用,你可以关闭它:
git config --global diff.noprefix true
回答by nert
These directory prefixes are there basically for compatibility and chosen as a sensible defaults. Explanation follows.
这些目录前缀基本上是为了兼容性,并被选为合理的默认值。解释如下。
Before git
在 git 之前
Before git (and other VCS), the workflow to create a patch for multiple files could have been, for example, the following:
在 git(和其他VCS)之前,为多个文件创建补丁的工作流程可能如下所示:
- Let's say you have a source code of a project
asdf
in a directoryasdf-source.latest
. - You copy the directory to a new directory (e.g.
asdf-source.new
, ideally hard-linkingthe files inside). - Now you can make all your changes in
asdf-source.new
, try to compile the code, test it, etc. - After you're done, you create a patch using e.g.
diff -r asdf-source.latest asdf-source.new >new_feature.patch
. The output evolved in time as well. Apart from other things, git uses an "unified" output by default, which might be acquired using diff's-u
parameter.
- 假设您
asdf
在目录中有一个项目的源代码asdf-source.latest
。 - 您将该目录复制到一个新目录(例如
asdf-source.new
,理想情况下硬链接其中的文件)。 - 现在您可以在 中进行所有更改
asdf-source.new
,尝试编译代码、测试它等。 - 完成后,您可以使用 eg 创建补丁
diff -r asdf-source.latest asdf-source.new >new_feature.patch
。输出也随着时间的推移而发展。除此之外,git 默认使用“统一”输出,可以使用 diff-u
参数获取。
Now you can see the patch has paths to changed files using the directory names.
现在您可以看到补丁具有使用目录名称更改文件的路径。
The person (or build script, and so on) applying your patch will then use patch
instead of using git apply
or git am
. In order for the command to find proper files, the directory name must be removed from the path, using patch's -pN
option (N shows the number of directory names and separators to remove). In the above case, the command used could be patch -p1 <new_feature.patch
. This makes it possible for the patch creator to use his/her own directory names.
应用您的补丁的人(或构建脚本等)将使用patch
而不是使用git apply
或git am
。为了让命令找到正确的文件,必须使用补丁的-pN
选项从路径中删除目录名(N 显示要删除的目录名和分隔符的数量)。在上述情况下,使用的命令可能是patch -p1 <new_feature.patch
. 这使得补丁创建者可以使用他/她自己的目录名称。
If you ever encounter a script patching some project using a lot of patches (usually used for back-portedpatches for stable package versions in Linux distributions for example), the patches may vary in the format.
The patch
command is able to detect those formats properly, but it is a little bit harder with the paths (how many directories to remove).
Some problems with that:
如果您遇到使用大量补丁(例如,通常用于Linux 发行版中稳定包版本的向后移植补丁)的脚本修补某个项目,则补丁的格式可能会有所不同。该patch
命令能够正确检测这些格式,但路径(要删除多少目录)有点困难。一些问题:
- Forcing developers to use 0 directories is not very nice.
- Having
patch
look for the file might be dangerous (as it may find a different file).
- 强迫开发者使用 0 个目录不是很好。
- 有
patch
查找文件可能是危险的(因为它可能会发现一个不同的文件)。
So having everyone sending patches that can be applied with patch -p1
seems the most sensible way to take.
因此,让每个人都发送可以应用的补丁patch -p1
似乎是最明智的做法。
Back to git
回到 git
When git was created, it adopted sensible defaults (which are compatible with most project's submission guidelines, mainly kernel) for such options. Thanks to this you are able to use git and send a properly formatted patch to someone who uses patch
to apply it and vice versa (git is able to handle diff
-created patches as well). Having "a" and "b" as prefixes in particular saves space (and a tiny percentage of bandwidth) while keeping everything working.
在创建 git 时,它为这些选项采用了合理的默认值(与大多数项目的提交指南,主要是内核兼容)。多亏了这一点,您可以使用 git 并将格式正确的补丁发送给使用patch
它的人,反之亦然(git 也能够处理diff
创建的补丁)。将“a”和“b”作为前缀尤其可以节省空间(和一小部分带宽),同时保持一切正常。
You can set git config diff.mnemonicprefix true
in order for git to use different prefixes depending on what you are comparing (see git help config
for further details).
您可以设置git config diff.mnemonicprefix true
以便 git 使用不同的前缀,具体取决于您要比较的内容(git help config
有关更多详细信息,请参见)。
回答by manojlds
It is to differentiate source and destination. You can also change it to be more meaningful:
它是区分源和目的地。您还可以将其更改为更有意义:
--src-prefix=
<prefix>
Show the given source prefix instead of "a/".
--dst-prefix=
<prefix>
Show the given destination prefix instead of "b/".
--src-前缀=
<prefix>
Show the given source prefix instead of "a/".
--dst-前缀=
<prefix>
Show the given destination prefix instead of "b/".
回答by arush436
If you want to add git diff --no-prefix
to your .gitconfigmanually, just add the following:
如果要手动添加git diff --no-prefix
到.gitconfig,只需添加以下内容:
[diff]
noprefix = true