git commit symlink as a regular file

2019-04-03 04:11发布

问题:

Suppose I have a file fname which is a symlink to a file from some other repository/project, say ../../proj2/fname.

Is there a way to add/commit fname as a regular file?

It seems that, by default, git gives the file mode 120000 and sets the path to the linked file as the blob content.

I know this because git ls-tree shows mode 120000 for the file, and git cat-file -p shows ../../proj2/fname as the blob's content.

回答1:

Nope, Git knows it's a symlink. It'd be kind of dangerous for Git to pretend otherwise, since it would then end up writing to files outside the repo. Tracking it as a symlink is exactly the intended behavior.



回答2:

If you want the file to appear instead of the link, you should probably use the ln command to create a hard-link instead of a sym-link (ln -s).

Making a hard-link, you can make the same file to appear under two different directories, so changing it via any of the links will reflect changes via both links, and the file will live in both directories, so it will be tracked by git.

I hope Windows' mklink /j command in Bukov's answer does this, but I really don't know at all.



回答3:

In Windows, you can do what you want with a Junction

For example, programs often keep a settings file somewhere on the system, but I'd like to version control it in my repository. I can't move the file, and I don't want to make duplicates or anything

If we put a Windows Shortcut in the repository directory though, he'll see it as a binary single file; not a directory pointing to all the actual files you want to include

What we need is the ability to put something like a Windows shortcut in the repository, but that git will treat as just another folder:

cd /location/of/my/repo/  
mklink /j "_linkTo_VimSettings" "C:\Program Files (x86)\Vim"


标签: git symlink