Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

ln

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
like image 911
user123456 Avatar asked Oct 08 '16 17:10

user123456


1 Answers

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.
like image 172
TeWu Avatar answered Oct 05 '22 23:10

TeWu