Suppose I have a file fname
which is a symlink to a file from some other repository/project, say ../../proj2/fname
.
Is there a way to add/commit fname
as a regular file?
It seems that, by default, git gives the file mode 120000 and sets the path to the linked file as the blob content.
I know this because git ls-tree
shows mode 120000 for the file, and git cat-file -p
shows ../../proj2/fname
as the blob's content.
Git can track symlinks as well as any other text files. After all, as the documentation says, a symbolic link is nothing but a file with special mode containing the path to the referenced file.
By default, the ln command creates hard links. To create a symbolic link, use the -s ( --symbolic ) option. If both the FILE and LINK are given, ln will create a link from the file specified as the first argument ( FILE ) to the file specified as the second argument ( LINK ).
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.
If you want the file to appear instead of the link, you should probably use the ln
command to create a hard-link instead of a sym-link (ln -s
).
Making a hard-link, you can make the same file to appear under two different directories, so changing it via any of the links will reflect changes via both links, and the file will live in both directories, so it will be tracked by git
.
I hope Windows' mklink /j
command in Bukov's answer does this, but I really don't know at all.
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