Linux Git 和硬链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3729278/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:11:04  来源:igfitidea点击:

Git and hard links

linuxgithyperlinkhardlink

提问by Alfredo Palhares

Considering that Git does not recognize symbolic links that point outside of the repository, is there any problem using hard links?

考虑到 Git 无法识别指向存储库外部的符号链接,使用硬链接有什么问题吗?

Could Git break them? Can you please point me to detailed information?

Git 能破解它们吗?你能指出我的详细信息吗?

采纳答案by Jakub Nar?bski

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 linkscannot be represented in git, at least not without third party tools such as metastoreor git-cache-meta(and I am not sure if it is possible even with those tools).

'tree' 对象,代表 Git 中的目录,存储文件名和(的子集)权限。它不存储 inode 编号(或其他类型的文件 ID)。因此,硬链接不能在 git 中表示,至少在没有第三方工具如Metastoregit-cache-meta 的情况下不能表示(我不确定即使使用这些工具是否可行)。

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.

Git 尽量不接触不需要更新的文件,但您必须考虑到 git 不会尝试保留硬链接,因此它们可能会被 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 outsidegit repository, and not under control of git.

关于指向存储库外部的符号链接:git 对它们没有问题,应该保留符号链接的内容……但我怀疑此类链接的实用性,因为这些符号链接是否会被破坏取决于git 存储库外部的文件系统布局,并且不受 git 的控制。

回答by VonC

From this msysgit issue

从这个msysgit 问题

Junction points are not symbolic links; therefore, symbolic links are simply unsupported in msysGit.

Also, hard links were never tracked by Git.

连接点不是符号链接;因此,msysGit 根本不支持符号链接。

此外,硬链接从未被 Git 跟踪

The issue were Windows-oriented (since it is about msysgit) and debate about the potential support of symlink.
But the comment about hard link concerns Git in general.

这个问题是面向 Windows 的(因为它是关于 msysgit)和关于符号链接的潜在支持的争论。
但是关于硬链接的评论一般与 Git 相关。

回答by xce_git

Google 'git preserve hard links' and it shows that git does not know how to preserve hard link structure AFAIK, perhaps by design.

谷歌'git保留硬链接'它表明git不知道如何保留硬链接结构AFAIK,也许是设计使然。

Web projects of mine use hard links as follows:

我的Web项目使用硬链接如下:

www/products/index.php
www/products/dell_latitude_id577/index.php #(hard linked to above)
www/products/dell_inspiron_id323/index.php #(hard linked again to above)

me@server:www/products$ ls -l index.php
-rwxr-xr-x 3 me me 1958 Aug 22 22:10 index.php*

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.

如果我想对 index.php 进行更改,我会在一个地方更改它,并且硬链接(产品详细信息页面)指向更改——除了 git 在克隆和拉取其他计算机期间不会保留这种关系。

me@server:www$ git pull

on another machine will create a new index.php for each hard link.

在另一台机器上将为每个硬链接创建一个新的 index.php。

回答by Niloct

I found out that, using hooks, you can capture the git pullevent (when there is something to pull...) writing the script event handler to .git/hooks/post-mergefile.

我发现,使用钩子,您可以捕获git pull事件(当有东西要拉时……)将脚本事件处理程序写入.git/hooks/post-merge文件。

First, you have to chmod +xit.

首先,你必须这样chmod +x做。

Then, put the lncommands inside it to recreate hard links at each pull. Neat huh!

然后,将ln命令放入其中以在每次拉动时重新创建硬链接。不错哦!

It works, I just needed that for my project and ls -ishows that files were automatically linked after pull.

它有效,我只需要我的项目并ls -i显示文件在pull.



My example of .git/hooks/post-merge:

我的例子.git/hooks/post-merge

#!/bin/sh
ln -f $GIT_DIR/../apresentacao/apresentacao.pdf $GIT_DIR/../capa/apresentacao.pdf
ln -f $GIT_DIR/../avaliacoesMono/avaliacao_monografias_2011_Nilo.pdf $GIT_DIR/../capa/avaliacoes.pdf
ln -f $GIT_DIR/../posters/poster_Nilo_sci.pdf $GIT_DIR/../capa/poster.pdf
ln -f $GIT_DIR/../monografia/monografia_Nilo.pdf $GIT_DIR/../capa/monografia_Nilo.pdf

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.

重要提示:如您所见,存储库中任何文件的路径都应以 开头$GIT_DIR,然后将部分相对路径添加到文件中。

Also important: -fis necessary, because you are recreating the destination file.

同样重要的是:-f是必要的,因为您正在重新创建目标文件。