Git 如何处理符号链接? [英] How does Git handle symbolic links?
问题描述
如果我有一个作为符号链接的文件或目录,并且我将它提交到 Git 存储库,它会发生什么?
If I have a file or directory that is a symbolic link and I commit it to a Git repository, what happens to it?
我认为它会将它作为符号链接保留,直到文件被删除,然后如果您从旧版本中拉回文件,它只会创建一个普通文件.
I would assume that it leaves it as a symbolic link until the file is deleted and then if you pull the file back from an old version it just creates a normal file.
当我删除它引用的文件时它会做什么?它只是提交悬空链接吗?
What does it do when I delete the file it references? Does it just commit the dangling link?
推荐答案
来自 linux 符号链接手册(假设您使用的是 Linux):
From linux symlink manual (assuming you are in Linux):
符号链接是一种特殊类型的文件,其内容是一个字符串,该字符串是另一个文件的路径名,链接所指向的文件.(符号链接的内容可以使用 readlink(2) 读取.)
A symbolic link is a special type of file whose contents are a string that is the pathname of another file, the file to which the link refers. (The contents of a symbolic link can be read using readlink(2).)
所以符号链接是一个多文件,就像一个 README.md
或一个 Makefile
.Git 只是将链接的内容(即它链接到的文件系统对象的上述路径)存储在一个blob"中,就像它存储任何其他文件一样.然后它将名称、模式和类型(包括它是一个符号链接这一事实)存储在表示其包含目录的树对象中.
So a symbolic link is one more file, just as a README.md
or a Makefile
. Git just stores the contents of the link (i.e. the aforementioned path of the file system object that it links to) in a 'blob' just like it would for any other file. It then stores the name, mode and type (including the fact that it is a symlink) in the tree object that represents its containing directory.
当您检出包含链接的树时,无论目标文件系统对象是否存在,它都会将对象恢复为符号链接.
When you checkout a tree containing the link, it restores the object as a symlink regardless of whether the target file system object exists or not.
如果您删除符号链接引用的文件,它不会以任何方式影响 Git 控制的符号链接.你会有一个悬空的参考.如果需要,用户可以删除或更改链接以指向有效的内容.
If you delete the file that the symlink references it doesn't affect the Git-controlled symlink in any way. You will have a dangling reference. It is up to the user to either remove or change the link to point to something valid if needed.
这篇关于Git 如何处理符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!