TL;DR: The data referenced by the symlink is not stored in the repository.
您可以通过查看将文件添加到索引时的功能来了解Git对文件执行的操作 . 索引就像预先提交一样 . 提交索引后,您可以使用 git checkout 将索引中的所有内容返回到工作目录中 . 那么,当您向索引添加符号链接时,Git会做什么?
要找出答案,首先要做一个符号链接:
$ ln -s /path/referenced/by/symlink symlink
Git还不知道这个文件 . git ls-files 允许您检查索引( -s 打印 stat -like输出):
$ git ls-files -s ./symlink
[nothing]
现在,通过将符号链接的内容添加到索引,将其添加到Git对象库中 . 将文件添加到索引时,Git将其内容存储在Git对象库中 .
$ git add ./symlink
那么,添加了什么?
$ git ls-files -s ./symlink
120000 1596f9db1b9610f238b78dd168ae33faa2dec15c 0 symlink
哈希是对在Git对象库中创建的压缩对象的引用 . 如果查看存储库根目录中的 .git/objects/15/96f9db1b9610f238b78dd168ae33faa2dec15c ,则可以检查此对象 . This file is what Git stores and what you can check out from your repository when you add and commit a symlink. 如果您检查此文件,您会发现它非常小 . 它不存储链接文件的内容 .
(注意 120000 是 ls-files 输出中列出的模式 . 对于常规文件,它类似于 100644 . )
但是当你从存储库和文件系统中检查出来时,Git会对这个对象做些什么呢?这取决于 core.symlinks 配置 . 来自man git-config:
core.symlinks如果为false,则将符号链接签出为包含链接文本的小型纯文本 .
因此,通过存储库中的符号链接,在结帐时,您将获得一个文本文件,其中包含对完整文件系统路径的引用或正确的符号链接,具体取决于 core.symlinks config的值 .
Either way, the data referenced by the symlink is not stored in the repository.