Git and hard links

后端 未结 4 656
囚心锁ツ
囚心锁ツ 2020-11-28 04:31

Considering that Git does not recognize symbolic links that point outside of the repository, is there any problem using hard links?

Could Git break them? Can you pl

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 05:20

    I found out that, using hooks, you can capture the git pull event (when there is something to pull...) writing the script event handler to .git/hooks/post-merge file.

    First, you have to chmod +x it.

    Then, put the ln commands inside it to recreate hard links at each pull. Neat huh!

    It works, I just needed that for my project and ls -i shows that files were automatically linked after pull.


    My example of .git/hooks/post-merge:

    #!/bin/sh
    ln -f $GIT_DIR/../apresentacao/apresentacao.pdf $GIT_DIR/../capa/apresentacao.pdf
    ln -f $GIT_DIR/../avaliacoesMono/avaliacao_monografias_2011_Nilo.pdf $GIT_DIR/../capa/avaliacoes.pdf
    ln -f $GIT_DIR/../posters/poster_Nilo_sci.pdf $GIT_DIR/../capa/poster.pdf
    ln -f $GIT_DIR/../monografia/monografia_Nilo.pdf $GIT_DIR/../capa/monografia_Nilo.pdf
    

    IMPORTANT: As you can see, the path to any file in your repository should begin with $GIT_DIR, then add the partial relative path to the file.

    Also important: -f is necessary, because you are recreating the destination file.

    EDIT

    Modern git client seems to support symlinks and hardlinks inside of the repository naturally, even when pushing to a remote location and then cloning from it. I never had the need again to link outside a git repo though...

    $ mkdir tmp
    $ cd tmp
    $ git --version
    git version 2.24.3 (Apple Git-128)
    $ git init .
    Initialized empty Git repository in /Users/teixeira/tmp/.git/
    $ mkdir x
    $ cd x
    $ echo 123 > original
    $ cat original
    123
    $ cd ..
    $ ln -s x/original symlink
    $ cat symlink
    123
    $ ln x/original hardlink
    $ cat hardlink
    123
    $ git add .
    $ git commit -m 'Symlink and hardlink commit'
    [master (root-commit) 8df3134] Symlink and hardlink commit
     3 files changed, 3 insertions(+)
     create mode 100644 hardlink
     create mode 120000 symlink
     create mode 100644 x/original
    

    Cloning from local git repository

    $ cd
    $ git clone tmp/ teste_tmp
    Cloning into 'teste_tmp'...
    done.
    $ cd teste_tmp/
    $ ls
    hardlink  symlink  x
    $ cat symlink
    123
    $ cat hardlink
    123
    

    Cloning from remote repository

    $ cd ~/tmp
    $ git remote add origin https://github.com/myUser/myRepo.git
    $ git push origin master
    Enumerating objects: 5, done.
    Counting objects: 100% (5/5), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (5/5), 361 bytes | 361.00 KiB/s, done.
    Total 5 (delta 0), reused 0 (delta 0)
    To https://github.com/myUser/myRepo.git
     + 964dfce...8df3134 master -> master
    $ cd ../
    $ git clone https://github.com/myUser/myRepo.git
    Cloning into 'myRepo'...
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
    Unpacking objects: 100% (5/5), done.
    $ cd myRepo/
    $ cat symlink
    123
    $ cat hardlink
    123
    

    https://github.com/mokacoding/symlinks also points an important thing: symlinks must be defined relatively.

提交回复
热议问题