2

I have a file with name "folder/hello.js" in GIT repository. It has 10 other duplicate files in same GIT repo. Every time I checkout and modify "hello.js" I have to manually change all other 10 duplicate files.

Is there any way to create symlink for "folder/hello.js" file and delete other 10 files.

jon
  • 213
  • 1
  • 5
  • 18
  • https://stackoverflow.com/a/18791647/9534591 Does this answer your question? – ik1ne Nov 12 '19 at 08:09
  • 1
    Does this answer your question? [How does Git handle symbolic links?](https://stackoverflow.com/questions/954560/how-does-git-handle-symbolic-links) – Ocaso Protal Nov 12 '19 at 08:15
  • 1
    no its not helping me. I am looking for a way to create symlinks in my local and commit them to GIT. When someone else clones the repo the symlinks should work fine – jon Nov 12 '19 at 09:21
  • 1
    you need to use `relative` symbolic links, e.g., `ln -s ../hello.js hello.js`. This way it will work in any clone as well. – Serge Nov 12 '19 at 11:46
  • Hi Serge - My file is exactly located at "C:\Users\nr\Desktop\calcmanager\CalcMgr\resources\resources\calcmgr/hello.js" can you help me to covert this path into relative. – jon Nov 12 '19 at 13:50
  • Hi Serge - this is how git repo path looks like "calcmanager\CalcMgr\resources\resources\calcmgr\hello.js" – jon Nov 12 '19 at 13:51
  • Sorry, could not fit an explanation in the comment, It is an answer about relative links down there. – Serge Nov 13 '19 at 02:56

1 Answers1

9

Symbolic links are just files containing symbolic pointer to other files. Git commits the contents of the symbolic link into its repo and clone and pull will recreate those symbolic links.

Symbolic links could contain absolute or relative paths to a file. A relative path starts at the directory where the link is located. So, imagine this directory/file struct

a -
  |- b -
       |- hello.js
  |- hello.js

you want to replace one of the files with a relative symbolic link to the another one.

Supposedly you decide that your real file is in a/hello.js and your link will be in a/b/hello.js. So, you would need to do 2 things in a linux-type shell:

rm a/b/hello.js # you need to replace it with a symbolic link
ln -s ../hello.js a/b/hello.js

in the dos shell

mklink a\b\hello.js ..\hello.js

Now the file a/b/hello.js will contain text ../hello.js. The file system will use the text to find the real file in the parent directory a/b.

Git will save the link pointer and when you clone the repo, the file system will see the the symbolic link to ..\hello.js

If you decide to use another file as a symbolic link, you can do

 ln -s b/hello.js hello.js

This way b/hello.js will be a relative symbolic link and will be substituted in the path instead of a/hello.js

Also, a symbolic link can have a name different from the original file. A symbolic link can point to another symbolic link as well in a chain-like sequence.

Hope it helps you with your problem.

TamusJRoyce
  • 817
  • 1
  • 12
  • 25
Serge
  • 11,616
  • 3
  • 18
  • 28