How does Git handle symbolic links?

后端 未结 3 629
鱼传尺愫
鱼传尺愫 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:44

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:52

    "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 讨论(0)
提交回复
热议问题