How does Git handle symbolic links?

后端 未结 3 628
鱼传尺愫
鱼传尺愫 2020-11-22 03:20

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 un

3条回答
  •  伪装坚强ぢ
    2020-11-22 03:48

    You can find out what Git does with a file by seeing what it does when you add it to the index. The index is like a pre-commit. With the index 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?

    To find out, 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 contents of the symbolic link to the Git object store by adding it to the index. When you add a file to the index, Git stores its contents in the Git 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 Git 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 data referenced by the symlink is not stored in the repository.

提交回复
热议问题