Can I add a file by putting the symbolic link in the git repository?
Asked Answered
U

2

5

Is there a way to push a file external to the git repository by putting a symbolic link.

I want for instance to push the file /root/my_file and have created a symbolic link my_symbolic_link in the git repository.

# ls -lA                                                                                                                    
drwxr-xr-x 7 root root 4096 Oct  8 07:55 .git/
lrwxrwxrwx 1 root root   26 Oct  8 7:58 my_symbolic_link -> /root/my_file
Unthread answered 8/10, 2016 at 17:25 Comment(6)
I think this will only add the link itself...Holocrine
By default yes, can I configure git to change the behaviorUnthread
I don't think so, even with hard links (see here).Holocrine
I checked it, and committing hard link to git repo works as expected. I tested it using git version 2.10.1 on Ubuntu 14.04; during test local repo was on ext4 partition, and remote was at BitBucket.Endurance
@Endurance "works as expected" might be too hasty. Have you tried to update the file somewhere else and pull? How about discarding local changes and checkout from your repo? I'm not saying you're wrong, but posts like this makes me doubt.Holocrine
@Holocrine "works as expected" wasn't very precise statement ;) So to avoid misunderstanding, I updated my answer.Endurance
E
13

Symbolic link is a file that contains a reference to another file in your filesystem. You can add symlink to your repository, but by doing it you are only adding reference to other file and not that other file.

You can achieve what you want by creating hard link instead of symbolic link. Hard link is association between name of the file and its content (and metadata) on file system level. By creating hard link to a file and adding it to your repository, you are adding that linked file. On POSIX-compliant operating systems (like all the linuxes) you can create hard link like this:

ln /root/my_file my_hard_link

For more info about different kind of links see this question and its answers.

UPDATE: Please note that git doesn't know anything about hard links. Adding hard link to git repo means that you are adding content of that linked file. Git doesn't know that the file you've just added is a hard link. Adding hard link to repo won't break it, but checking it out of the repo will create a new copy of that file, and won't recreate that hard link. By "checking it out" I don't mean only git checkout command, but also operations like cloneing a new copy of the repo, deleting hard link and using git reset to recreate it, or pulling new version of this hard linked file from remote repo.

To recap - your two options are:

  • Add symbolic link to the repo - this will add only information about the link, and not content of the linked file.
  • Add hard link to the repo - this will add content of the linked file. Although it won't add information about the link, it also won't break the link until next time you check that hard linked file out from the repo.
Endurance answered 8/10, 2016 at 17:29 Comment(0)
N
0

If you are on Windows you may try a way to create the symbolic link in the git repository using Git for Windows.

Starting with Windows Vista, there is support for symbolic links. You have to Enable Symbolik Link on Git during the application Setup (see picture)

By default, the ln -s command in Git Bash does not create symbolic links. Instead, it creates copies.

To create symbolic links (provided your account has permission to do so), use the built-in mklink command, like so:

mklink /d this-link-points-to c:\that-directory
mklink this-link-points-to c:\that-file

Enable Symbolik Link on Git

Noncommittal answered 25/4, 2018 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.