80

在 git 中处理符号链接的正确方法是什么?

我有以下结构:

Vendors
  Module A
  Module B
  Module C
App
 Code
   Modules
     Core Module 1
     Core Module 2
     Module A (symlinked to vendors)
     Module B (symlinked to vendors)
     Module C (symlinked to vendors)

有一个主 App 目录,其中包含应用程序中的所有核心代码。此外,还有一个供应商目录,其中包含符号链接到主应用程序目录并因此集成的模块。

重要的是,供应商目录和主应用程序目录都在同一个存储库中进行版本控制。

因此,我应该让 git 继续存储符号链接,还是想办法让它忽略符号链接?

4

3 回答 3

144

Git can handle symlinks just fine as long as the operating system used by all developers supports them. Since you depend on having these symlinks present I will assume that your development environments all support symlinks.

To decide if something should be included in your git repository or not (symlink or otherwise) consider the following:

  • Is it a file generated by some tool or other process in the repository? If so, it's best to ignore it and let each user generate the file, so that they will always have the latest version.
  • Is the file specific to a particular user's development environment, or one that is used in all environments? If it's a quirk of a particular user's environment, like a configuration to ignore Emacs backup files, it doesn't belong in the repo. If it's something all developers will need, and/or something that's needed to build the application for production, it should go in the repository.

In your case it seems like the symlinks are not generated and they are needed in all environments, so putting them in the repository should be fine.

However, when creating them be sure to create them as relative symlinks rather than absolute symlinks, so that they'll work regardless of where the repository is cloned. The easiest way to do this is to change directory into the Modules directory and create the symlink from there:

cd App/Code/Modules
ln -s "../../../Vendors/Module A" "Module A"
于 2013-03-17T20:01:20.817 回答
13

Git stores the symlink, just like any other file in its version control - except for a symlink it would only store the information about the path it is symlinking to, and the file type as symlink instead of a regular file.

If the symlink points to a directory, git does not store the contents under the symlinked directory.

So there should be no harm in storing symlinks versioned under git for your case.

One more thing you need to be aware of with symlinks is that, git would only recreate the symlinks on a fresh clone, not the file or directory it is pointing at. There are chances that the symlinked path would be non-existent (say when using absolute paths).

于 2013-03-17T19:59:01.337 回答
1

@Tuxdude 完全不能同意你的观点“......那么你做错了什么”。例如,如果您需要将媒体文件夹放在网络服务器或 NFS 上的不同驱动器上,那么您必须将其置于版本控制之外。因此,正如您所解释的,将无法通过版本控制访问符号链接媒体文件夹中的内容。但这是一个你必须这样做的场景。这真的很痛苦......我的场景更加复杂(我不会详细介绍),我真正想要的是将符号链接文件夹的子文件夹添加到版本控制中,而不是它的内容,但是我需要有一个选项,我可以忽略 git 中子文件夹类型本身的任何更改。例如,基本结构:

  • 应用程序/媒体/bla
  • 应用程序/媒体/blub

我需要 git 版本控制中的那些文件夹,没有它的内容。

在网络服务器(相同版本)上,这些文件夹看起来像这样(符号链接):

  • app/media/bla => 完全在别处
  • app/media/blubb => 又在另一个地方

开发人员在他们的本地环境中应该只有第一步中解释的原始版本化结构(无符号链接)。但是网络服务器有指向不同 NFS 系统的符号链接。

如果有人知道如何解决这个问题,我将不胜感激,因为我还没有找到任何解决方案。

我现在这样做的唯一方法是让一个构建器为本地环境创建正确/不同的结构,并且服务器和媒体子文件夹目前完全被 gitignore 忽略。但这有时可能很棘手/难以维护。

于 2016-05-25T00:29:55.060 回答