Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git commit symlink as a regular file

Tags:

git

symlink

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.

like image 749
hasen Avatar asked Dec 12 '11 05:12

hasen


People also ask

Can you commit a symlink to Git?

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.

How do you symlink a 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 ).

Should I enable symbolic links in git?

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.

How do I remove a symbolic link in git?

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.


1 Answers

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.

like image 58
mgarciaisaia Avatar answered Sep 21 '22 05:09

mgarciaisaia