You can see what Git does with a symbolic link by adding it to the index. The index is like a pre-commit. When the index is committed, you can use git checkout
to bring everything that was in the index back into the working directory.
So, what does Git do when you add a symbolic link to the index?
First, make a symbolic link:
$ ln -s /path/referenced/by/symlink symlink
Git doesn't know about this file yet. git ls-files
lets you inspect your index (-s
prints stat
-like output):
$ git ls-files -s ./symlink
[nothing]
Now, add the symbolic link to the index. When you add a file to the index, Git copies its contents in the object store.
$ git add ./symlink
What was added?
$ git ls-files -s ./symlink
120000 1596f9db1b9610f238b78dd168ae33faa2dec15c 0 symlink
The hash is a reference to the packed object that was created in the object store. You can examine this object if you look in .git/objects/15/96f9db1b9610f238b78dd168ae33faa2dec15c
in the root of your repository. This is the file that Git stores in the repository, that you can later check out. If you examine this file, you'll see it is very small. It does not store the contents of the linked file. To confirm this, print the contents of the packed repository object with git cat-file
:
$ git cat-file -p 1596f9db1b9610f238b78dd168ae33faa2dec15c
/path/referenced/by/symlink
(Note 120000
is the mode listed in ls-files
output. It would be something like 100644
for a regular file.)
But what does Git do with this object when you check it out from the repository and into your filesystem? It depends on the core.symlinks
config. From man git-config
:
core.symlinks
If false, symbolic links are checked out as small plain files that contain the link text.
So, with a symbolic link in the repository, upon checkout you either get a text file with a reference to a full filesystem path, or a proper symbolic link, depending on the value of the core.symlinks
config.
Either way, the content of the path referenced by the symlink is not stored in the repository (unless the referenced path is also in the repository, of course).