1787
votes

If I have a file or directory that is a symbolic link and I commit it to a Git repository, what happens to it?

I would assume that it leaves it as a symbolic link until the file is deleted and then if you pull the file back from an old version it just creates a normal file.

What does it do when I delete the file it references? Does it just commit the dangling link?

4
.gitignore sees the symlink as a file not a folder.0xcaff
Well, evidently there's more to the question than that answer implies. For instance, I'm wondering the following: if I create a sym link in my repository to some large file in that repository, push the changes, and then pull those changes to another machine, what will happen? Will the large file be stored as a large file in both locations, or will the sym link be preserved, such that on the new machine, the link file points to the original large file?jvriesem
This is is an old thread but this comment may still be useful. In response to jviesem, a soft link is basically a file with the name of another file. So once you pull it to a different machine, the link will be downloaded and it will have the name of the big file on the original file system. If on the new machine the name isn't valid, then then link will have a invalid name. The big file will not be downloaded to the new machine.lasaro
@lasaro, the way to avoid broken links in a git repo is to always use relative paths when making the symlinks, using ../.. as needed.Wildcard
Notice that in most versions of Windows you need elevated permissions in order to create a symlink. If you're on Windows and git pull creates a file instead of symlink, try to run you Git client as administrator.axmrnv

4 Answers

1477
votes

Git just stores the contents of the link (i.e. the path of the file system object that it links to) in a 'blob' just like it would for a normal 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.

When you checkout a tree containing the link, it restores the object as a symlink regardless of whether the target file system object exists or not.

If you delete the file that the symlink references it doesn't affect the Git-controlled symlink in any way. You will have a dangling reference. It is up to the user to either remove or change the link to point to something valid if needed.

324
votes

You can find out what Git does with a symbolic link by seeing what it does when you add 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

So, 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).

151
votes

"Editor's" note: This post may contain outdated information. Please see comments and this question regarding changes in Git since 1.6.1.

Symlinked directories:

It's important to note what happens when there is a directory which is a soft link. Any Git pull with an update removes the link and makes it a normal directory. This is what I learnt hard way. Some insights here and here.

Example

Before

 ls -l
 lrwxrwxrwx 1 admin adm   29 Sep 30 15:28 src/somedir -> /mnt/somedir

git add/commit/push

It remains the same

After git pull AND some updates found

 drwxrwsr-x 2 admin adm 4096 Oct  2 05:54 src/somedir
0
votes

Special case: When "git checkout"(man) removes a path that does not exist in the commit it is checking out, it wasn't careful enough not to follow symbolic links, which has been corrected with Git 2.32 (Q2 2021).

See commit fab78a0, commit 462b4e8 (18 Mar 2021) by Matheus Tavares (matheustavares).
(Merged by Junio C Hamano -- gitster -- in commit 9210c68, 30 Mar 2021)

checkout: don't follow symlinks when removing entries

Signed-off-by: Matheus Tavares

At 1d718a5 ("do not overwrite untracked symlinks", 2011-02-20, Git v1.7.5-rc0 -- merge), symlink.c:check_leading_path() started returning different codes for FL_ENOENT and FL_SYMLINK.
But one of its callers, unlink_entry(), was not adjusted for this change, so it started to follow symlinks on the leading path of to-be-removed entries.
Fix that and add a regression test.

And because we no longer try to unlink such paths, we also don't get the warning from remove_or_warn().

For the regular file and symlink cases, it's questionable whether the warning was useful in the first place: unlink_entry() removes tracked paths that should no longer be present in the state we are checking out to.
If the path had its leading dir replaced by another file, it means that the basename already doesn't exist, so there is no need for a warning.
Sure, we are leaving a regular file or symlink behind at the path's dirname, but this file is either untracked now (so again, no need to warn), or it will be replaced by a tracked file during the next phase of this checkout