Git 和硬链接
考虑到Git不能识别指向存储库之外的符号链接,那么使用硬链接有什么问题吗?
Git 能破坏它们吗? 你能指点我详细的信息吗?
Considering that Git does not recognize symbolic links that point outside of the repository, is there any problem using hard links?
Could Git break them?
Can you please point me to detailed information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“树”对象代表 Git 中的目录,存储文件名和(子集)权限。它不存储 inode 号(或其他类型的文件 ID)。因此硬链接不能在git中表示,至少没有第三方工具,例如metastore 或 git-cache-meta (我不确定即使使用这些工具是否也可能)。
Git 尝试不触及不需要更新的文件,但您必须考虑到 git 不会尝试保留硬链接,因此它们可能会被 git 破坏。
关于指向外部存储库的符号链接:git 对它们没有任何问题,并且应该保留符号链接的内容...但此类链接的实用性对我来说是可疑的,因为这些符号链接是否会被破坏取决于在文件系统布局外部 git 存储库上,并且不受 git 控制。
The 'tree' object, representing directories in Git, stores file name and (subset of) permissions. It doesn't store inode number (or other kind of file id). Therefore hard links cannot be represented in git, at least not without third party tools such as metastore or git-cache-meta (and I am not sure if it is possible even with those tools).
Git tries to not touch files that it doesn't need to update, but you have to take into account that git doesn't try to preserve hardlinks, so they can be broken by git.
About symbolic links pointing outside repository: git has no problems with them and should preserve contents of symbolic links... but utility of such links is dubious to me, as whether those symlinks would be broken or not depends on the filesystem layout outside git repository, and not under control of git.
我发现,使用钩子,您可以捕获 git pull 事件(当有东西要拉时...)将脚本事件处理程序写入 .git/hooks/post-merge 文件。
首先,您必须
chmod +x
它。然后,将 ln 命令放入其中,以便在每次拉取时重新创建硬链接。整齐啊!
它有效,我只需要为我的项目使用它,并且
ls -i
显示文件在pull
后自动链接。我的
.git/hooks/post-merge
示例:重要提示:如您所见,存储库中任何文件的路径都应以
$GIT_DIR
开头,然后添加文件的部分相对路径。同样重要的是:
-f
是必要的,因为您正在重新创建目标文件。编辑
现代 git 客户端似乎自然地支持存储库内部的符号链接和硬链接,即使在推送到远程位置然后从中克隆时也是如此。不过,我再也不需要链接到 git 存储库之外了...
从本地 git 存储库克隆
从远程存储库克隆
https: //github.com/mokacoding/symlinks 还指出了一件重要的事情:符号链接必须相对定义。
I found out that, using hooks, you can capture the
git pull
event (when there is something to pull...) writing the script event handler to.git/hooks/post-merge
file.First, you have to
chmod +x
it.Then, put the
ln
commands inside it to recreate hard links at each pull. Neat huh!It works, I just needed that for my project and
ls -i
shows that files were automatically linked afterpull
.My example of
.git/hooks/post-merge
:IMPORTANT: As you can see, the path to any file in your repository should begin with
$GIT_DIR
, then add the partial relative path to the file.Also important:
-f
is necessary, because you are recreating the destination file.EDIT
Modern git client seems to support symlinks and hardlinks inside of the repository naturally, even when pushing to a remote location and then cloning from it. I never had the need again to link outside a git repo though...
Cloning from local git repository
Cloning from remote repository
https://github.com/mokacoding/symlinks also points an important thing: symlinks must be defined relatively.
从此
msysgit
问题关于硬链接的评论总体上涉及 Git。具体问题是面向 Microsoft Windows 的(因为它是关于 msysgit 的),并且讨论争论了符号链接的潜在支持。
From this
msysgit
issueThe comment about hard links concerns Git in general. The specific issue was Microsoft Windows-oriented (since it was about
msysgit
), and the discussion debates the potential support of symbolic links.谷歌“git 保留硬链接”,它表明 git 不知道如何保留硬链接结构 AFAIK,也许是有意设计的。
我的 Web 项目使用硬链接如下:
如果我想对 index.php 进行更改,我会在一处更改它,硬链接(产品详细信息页面)指向更改 - 除非 git 在克隆过程中不保留这种关系并拉动其他计算机。
在另一台机器上将为每个硬链接创建一个新的index.php。
Google 'git preserve hard links' and it shows that git does not know how to preserve hard link structure AFAIK, perhaps by design.
Web projects of mine use hard links as follows:
If I wanted to make changes to index.php I change it in one place and the hard links (product detail pages) point to the changes -- except git does not preserve this relationship during cloning and pulling on other computers.
on another machine will create a new index.php for each hard link.