What is the correct way to handle symlinks in git?
I have the following structure:
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)
There is a a main App directory which contains all of the core code in the application. Additionally there is a vendor directory that contains modules which get symlinked into the main app directory and therefore integrated.
Importantly, both the vendor directory and the main app directory are both versioned in the same repository.
Therefore, should i let git keep storing the symlinks, or find a way to have it ignore symlinks?
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.
Git will handle a hard link like a copy of the file, except that the contents of the linked files change at the same time. Git may see changes in both files if both the original file and the hard link are in the same repository.
Although Git supports symlinks, I would strongly recommend against storing them as links in your repository, especially if you're also working with that code on Windows.
The best way to remove a symlink is with the appropriately named “unlink” tool. Using unlink to delete a symlink is extremely simple, you just need to point it at the symbolic link to unlink and remove. As always with the command line, be sure your syntax is precise.
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:
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"
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With