Git Diff Investigation of Symbolic Links and Relative Path Symbolic Links

13 min

language: ja bn en es hi pt ru zh-cn zh-tw

Hello, I'm incompetent.
I was curious what would happen when creating a symbolic link with git, so I'm going to try it.

Patterns

  1. Hard link to /usr/bin/pwd: hardlink-pwd
  2. Symbolic link to /usr/bin/pwd: ln-s-link-pwd-usr-bin-pwd
  3. Text file named ./pwd
  4. Symbolic link to ./pwd: pwd-ln-s

I was curious what would happen if a hard link to an executable binary was git pushed and then git cloned (I can somewhat guess without doing it, but...). Also, I was curious if symbolic links would be maintained even if they were in an upper directory managed by git, so I'm going to try it.

Evidence

alleycat:[haturatu]:~/git$ mkdir gitlinks
alleycat:[haturatu]:~/git$ cd gitlinks/
alleycat:[haturatu]:~/git/gitlinks$ ls
alleycat:[haturatu]:~/git/gitlinks$ pwd > pwd
alleycat:[haturatu]:~/git/gitlinks$ ls
pwd
alleycat:[haturatu]:~/git/gitlinks$ cat pwd 
/home/haturatu/git/gitlinks
alleycat:[haturatu]:~/git/gitlinks$ ln -s pwd pwd-ln-s
alleycat:[haturatu]:~/git/gitlinks$ ls
pwd  pwd-ln-s
alleycat:[haturatu]:~/git/gitlinks$ which pwd
/usr/bin/pwd
alleycat:[haturatu]:~/git/gitlinks$ ln /usr/bin/p
Display all 354 possibilities? (y or n)
alleycat:[haturatu]:~/git/gitlinks$ ln /usr/bin/pw
pw-cli            pw-dump           pw-profiler       pwconv            pwmconfig
pw-config         pw-link           pw-reserve        pwd               pwunconv
pw-container      pw-metadata       pw-top            pwdx              
pw-dot            pw-mon            pwck              pwhistory_helper  
alleycat:[haturatu]:~/git/gitlinks$ ln /usr/bin/pwd hardlink-pwd
ln: 'hardlink-pwd' から '/usr/bin/pwd' へのハードリンクの作成に失敗しました: 許可されていない操作です
alleycat:[haturatu]:~/git/gitlinks$ sudo ln /usr/bin/pwd hardlink-pwd
指紋読取装置に右の人差し指をなぞってください
alleycat:[haturatu]:~/git/gitlinks$ ls
hardlink-pwd  pwd  pwd-ln-s
alleycat:[haturatu]:~/git/gitlinks$ ls -la
合計 48
drwxr-xr-x   2 haturatu haturatu  4096  1月 13 01:50 .
drwxr-xr-x 115 haturatu haturatu  4096  1月 13 01:48 ..
-rwxr-xr-x   2 root     root     34944  8月 31 23:29 hardlink-pwd
-rw-r--r--   1 haturatu haturatu    28  1月 13 01:48 pwd
lrwxrwxrwx   1 haturatu haturatu     3  1月 13 01:49 pwd-ln-s -> pwd
alleycat:[haturatu]:~/git/gitlinks$ chown haturatu:haturatu hardlink-pwd 
chown: 'hardlink-pwd' の所有者を変更中: 許可されていない操作です
alleycat:[haturatu]:~/git/gitlinks$ sudo chown haturatu:haturatu hardlink-pwd 
alleycat:[haturatu]:~/git/gitlinks$ which pwd
/usr/bin/pwd
alleycat:[haturatu]:~/git/gitlinks$ ls -la /usr/bin/pwd
-rwxr-xr-x 2 haturatu haturatu 34944  8月 31 23:29 /usr/bin/pwd
alleycat:[haturatu]:~/git/gitlinks$ echo "# git-ln-s-test" >> README.md
alleycat:[haturatu]:~/git/gitlinks$ git init
git add README.md
git commit -m "first commit"
git branch -M main
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/haturatu/git/gitlinks/.git/
[master (root-commit) 3853932] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
alleycat:[haturatu]:~/git/gitlinks$ git remote add origin git@github.com:haturatu/git-ln-s-test.git
alleycat:[haturatu]:~/git/gitlinks$ vim pwd
alleycat:[haturatu]:~/git/gitlinks$ git add .
alleycat:[haturatu]:~/git/gitlinks$ git commit -m "yo"
[main ca78a3e] yo
 3 files changed, 3 insertions(+)
 create mode 100755 hardlink-pwd
 create mode 100644 pwd
 create mode 120000 pwd-ln-s
alleycat:[haturatu]:~/git/gitlinks$ vim pwd-ln-s 
alleycat:[haturatu]:~/git/gitlinks$ git commit -m "2"
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   pwd

no changes added to commit (use "git add" and/or "git commit -a")
alleycat:[haturatu]:~/git/gitlinks$ git add .
alleycat:[haturatu]:~/git/gitlinks$ git commit -m "2"
[main b3fd4ed] 2
 1 file changed, 1 insertion(+)
alleycat:[haturatu]:~/git/gitlinks$ ls -la
合計 56
drwxr-xr-x   3 haturatu haturatu  4096  1月 13 01:53 .
drwxr-xr-x 115 haturatu haturatu  4096  1月 13 01:48 ..
drwxr-xr-x   9 haturatu haturatu  4096  1月 13 01:54 .git
-rw-r--r--   1 haturatu haturatu    16  1月 13 01:53 README.md
-rwxr-xr-x   2 haturatu haturatu 34944  8月 31 23:29 hardlink-pwd
-rw-r--r--   1 haturatu haturatu    38  1月 13 01:54 pwd
lrwxrwxrwx   1 haturatu haturatu     3  1月 13 01:49 pwd-ln-s -> pwd
alleycat:[haturatu]:~/git/gitlinks$  ln -s /usr/bin/pwd ln-s-link-pwd-usr-bin-pwd
alleycat:[haturatu]:~/git/gitlinks$ git add .
alleycat:[haturatu]:~/git/gitlinks$ git commit -m "3"
[main 119febf] 3
 1 file changed, 1 insertion(+)
 create mode 120000 ln-s-link-pwd-usr-bin-pwd
alleycat:[haturatu]:~/git/gitlinks$ git diff
alleycat:[haturatu]:~/git/gitlinks$ git push -u origin main
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (14/14), 13.37 KiB | 4.46 MiB/s, done.
Total 14 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:haturatu/git-ln-s-test.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
alleycat:[haturatu]:~/git/gitlinks$ ls -la
合計 56
drwxr-xr-x   3 haturatu haturatu  4096  1月 13 01:55 .
drwxr-xr-x 115 haturatu haturatu  4096  1月 13 01:48 ..
drwxr-xr-x   9 haturatu haturatu  4096  1月 13 01:56 .git
-rw-r--r--   1 haturatu haturatu    16  1月 13 01:53 README.md
-rwxr-xr-x   2 haturatu haturatu 34944  8月 31 23:29 hardlink-pwd
lrwxrwxrwx   1 haturatu haturatu    12  1月 13 01:55 ln-s-link-pwd-usr-bin-pwd -> /usr/bin/pwd
-rw-r--r--   1 haturatu haturatu    38  1月 13 01:54 pwd
lrwxrwxrwx   1 haturatu haturatu     3  1月 13 01:49 pwd-ln-s -> pwd
alleycat:[haturatu]:~/git/gitlinks$ sudo chown root:root hardlink-pwd 
指紋読取装置に右の人差し指をなぞってください
指紋の検証に失敗しました
指紋読取装置に右の人差し指をなぞってください
指紋の検証に失敗しました
指紋読取装置に右の人差し指をなぞってください
alleycat:[haturatu]:~/git/gitlinks$ cd ..
alleycat:[haturatu]:~/git$ rm -rf gitl
gitlercuko/ gitlinks/   
alleycat:[haturatu]:~/git$ rm -rf getlinks
alleycat:[haturatu]:~/git$ git clone git@github.com:haturatu/git-ln-s-test.git
Cloning into 'git-ln-s-test'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 14 (delta 2), reused 14 (delta 2), pack-reused 0 (from 0)
Receiving objects: 100% (14/14), 13.37 KiB | 6.69 MiB/s, done.
Resolving deltas: 100% (2/2), done.
alleycat:[haturatu]:~/git$ cd git-ln-s-test/
alleycat:[haturatu]:~/git/git-ln-s-test$ ls -la
合計 56
drwxr-xr-x   3 haturatu haturatu  4096  1月 13 01:59 .
drwxr-xr-x 116 haturatu haturatu  4096  1月 13 01:59 ..
drwxr-xr-x   8 haturatu haturatu  4096  1月 13 01:59 .git
-rw-r--r--   1 haturatu haturatu    16  1月 13 01:59 README.md
-rwxr-xr-x   1 haturatu haturatu 34944  1月 13 01:59 hardlink-pwd
lrwxrwxrwx   1 haturatu haturatu    12  1月 13 01:59 ln-s-link-pwd-usr-bin-pwd -> /usr/bin/pwd
-rw-r--r--   1 haturatu haturatu    38  1月 13 01:59 pwd
lrwxrwxrwx   1 haturatu haturatu     3  1月 13 01:59 pwd-ln-s -> pwd

Results

It seems that hard links are indeed unlinked, and since the inode number also changes, they are treated as separate files.

And with git log, you can check on the GUI by looking at my pushed logs, only the original file of the symbolic link shows diffs, but the symbolic link itself still doesn't show diffs.

It's fine if you specify a relative path when creating it.

alleycat:[haturatu]:~/git/gitlinks$ ln -s ../../../../usr/bin/pwd pwd-rel
alleycat:[haturatu]:~/git/gitlinks$ ls -la
合計 56
drwxr-xr-x   3 haturatu haturatu  4096  1月 13 02:16 .
drwxr-xr-x 116 haturatu haturatu  4096  1月 13 01:59 ..
drwxr-xr-x   9 haturatu haturatu  4096  1月 13 01:56 .git
-rw-r--r--   1 haturatu haturatu    16  1月 13 01:53 README.md
lrwxrwxrwx   1 haturatu haturatu    12  1月 13 02:08 ext4magic -> ../ext4magic
-rwxr-xr-x   2 root     root     34944  8月 31 23:29 hardlink-pwd
lrwxrwxrwx   1 haturatu haturatu    12  1月 13 01:55 ln-s-link-pwd-usr-bin-pwd -> /usr/bin/pwd
-rw-r--r--   1 haturatu haturatu    38  1月 13 01:54 pwd
lrwxrwxrwx   1 haturatu haturatu     3  1月 13 01:49 pwd-ln-s -> pwd
lrwxrwxrwx   1 haturatu haturatu    23  1月 13 02:16 pwd-rel -> ../../../../usr/bin/pwd

Related Posts