how to create symlink for files in GIT repository
Asked Answered
E

1

5

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.

Enter answered 12/11, 2019 at 8:2 Comment(7)
https://mcmap.net/q/44995/-how-does-git-handle-symbolic-links Does this answer your question?Revivalism
Does this answer your question? How does Git handle symbolic links?Slippage
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 fineEnter
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.Sequestrate
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.Enter
Hi Serge - this is how git repo path looks like "calcmanager\CalcMgr\resources\resources\calcmgr\hello.js"Enter
Sorry, could not fit an explanation in the comment, It is an answer about relative links down there.Sequestrate
S
13

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.

Sequestrate answered 13/11, 2019 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.