I have two folders:

  • /home/me/code/project/
  • /srv/www/projectfiles/

In the /home/me/code/project folder, I have symlinked static to /srv/www/projectfiles/

i.e. /home/me/code/project/static/ -> /srv/www/projectfiles/

When I try to commit now, it doesn't see any of the files behind the symlink, and instead tries to commit the symlink itself as a file.

How do I commit a file (e.g. /srv/www/projectfiles/style.css)s that is behind the symlink?


A workaround would be to have /srv/www/projectfiles be a symlink to /home/me/code/project/static so git sees no symlinks

  • 2
    So you're basically reversing the logic? Keep the required files/folders in the Git folder and then put a symlink elsewhere (where the original files/folders used to reside)? – ruslaniv Feb 15 '19 at 11:47
  • @Rusl Yes exactly that – mmmmmm Feb 15 '19 at 12:22
  • But then how can I do local customization of the files? I should have separate repo for each machine? Loses the whole point. – Ashnur Aug 28 '19 at 7:38
  • Well the local customisation won't be in git – mmmmmm Aug 28 '19 at 8:39
  • Except when the other service also takes this attitude. – HappyFace Feb 28 at 18:59

If you are using linux, I particularly like the solution provided by GitBLSR. It is a library that is loaded via LD_PRELOAD that transparently dereferences symlinks to files and folders outside a repository.

To install it for a local user account is simple:

git clone https://github.com/Alcaro/GitBSLR.git
cd GitBSLR

This will compile the library and create an alias like the following in ~/.bashrc:

alias git="LD_PRELOAD=/path/to/gitbslr.so git"

Using this alias enables the transparent link dereferencing.

  • 2
    That's terrible! But it works fine, so it's great. Thank you! – kay Nov 22 '18 at 15:51
  • That's weird! I plain love it – Marcos Besteiro López Mar 8 '19 at 15:25
  • 2
    As a git and linux noob without intuition for what good practices are, why is this terrible and weird? Is it because the command git is now kind of 'adulterated' and that is hacky and not pure/clean/neat? – perilousGourd Apr 30 '20 at 23:48
  • this is awesomely weird, but it worked like a charm – mariofix 2 days ago

Consider using a mount point to mount the destination folder (which you presently symlink to) to appear in the location you want it beneath the git project. I've used this approach successfully.


Another workaround - the only one I found that works for directories too - is to change git working tree for the specific actions.

git --work-tree=/home/me/code/project/ add /home/me/code/project/static/
git --work-tree=/home/me/code/project/ commit /home/me/code/project/static/

Move the files into the folder and remove the symlink. Git doesn't traverse symlinks.

It (and I) would assume that you are symlinking to a set of files that you either don't control or don't want to modify, as such they don't need to be versioned.

  • 1
    I do control these files. They are the stylesheets etc. and need to be in a directory which the server will server. – Macha Aug 11 '10 at 19:13
  • See my solution which basically does this in an automated way. – anthony Feb 21 '20 at 0:39

I had the same problem... Uploading a directory with symlinked files into a GIT repository, for public release.

The GIT repository is secondary source, not the primary source of the files, as such I do not want to replace the symlinks on my local machine. Hardlinks are no good as these get broken, and it is not obvious when looking that they are links.

My solution script was posted here... https://stackoverflow.com/a/60330575/701532


Do rsync <folder_out_of_git_repo> <folder_in_git_repo> before git add && git commit.

  • I like this idea, which could be in a git hook or done by a Makefile, etc.. However, if the filesystem supports it (and all these files are in the same filesystem) a hard link would work for the files, but not directories. – Jack Wasey Jul 8 '20 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.