How can I make git commit files in a symlinked folder
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?
Solution 1:
A workaround would be to have /srv/www/projectfiles be a symlink to /home/me/code/project/static so git sees no symlinks
Solution 2:
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
./install.sh
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.
Solution 3:
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.
Example:
#!/bin/sh
sudo mount --bind ./src_folder ./dst_folder
Solution 4:
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/