23

Somebody already asked how you can get git to follow symlinks. There was an answer for a symlinked directory, but not for a symlinked file. It was also over a year ago.

Question: how do you get git to follow a symlink and add the file it refers to?

Here is the old question: How can I get git to follow symlinks?. There's also a question about what git typically does How does git handle symbolic links?. I'm after a way of changing this behaviour.

In case you care: I'm running git 1.5.4.3 on unix and git version 1.6.0 on mac.

Community
  • 1
  • 1
Peter
  • 127,331
  • 53
  • 180
  • 211
  • For me, using mount as explained in https://stackoverflow.com/questions/86402/how-can-i-get-git-to-follow-symlinks is a very useful solution. – meolic Sep 23 '20 at 11:41

5 Answers5

24

I'm pretty sure there's no way.

Additionally, it sounds like a kind of insecure, undefined behavior - what should it do when you move between versions of the file and it needs to write to it? In particular, if you check out a revision before it was added, do you really want it to remove the contents of a file outside the repository? What happens if you come back to present and recreate the file, or if the symlink itself is modified - should git also track the symlink itself?

Things along these lines were said on the git mailing list late last year in response to essentially the same question.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 2
    Yeah, unfortunately. I know it'd be really nice to follow links in some cases. There was an approach suggested in one of the replies there - make the outside-the-repo files symlinks into the repo, and possibly still include symlinks to them from within the repo. Hope you find a way to do what you need to! – Cascabel Sep 30 '09 at 21:49
13

You can use hardlinks instead of softlinks (a.k.a. symlinks). Git will then see the contents of the linked file. The disadvantage is that when someone checks out, the file is created as a normal file in the checked-out directory, because Git does not understand it as a link.

Marios Zindilis
  • 201
  • 3
  • 2
6

how about using hard links, then git has no idea its a linked file (does it?)

edmondscommerce
  • 2,001
  • 12
  • 21
2

The trouble with using hardlinks is that if something writing to the other location replaces the file, rather than just writing changes to it, then the destination file has a new inode on the filesystem and the hardlink no longer points to it, so the files are out of sync.

a8ksh4
  • 33
  • 4
0

I have several different repositories and they all use a common set of files -- typically .h files. I only want one version of all the common files to exist, which is a common use case for links. I need git to keep the common contents of these files in each repository directory that needs those files. This only seems to work with hard links, but they aren't so great because when I checkout an earlier version in one of the repository directories, that directory gets an actual copy of the common files and no longer shares with the other repositories. So I have a shell script to restore the hard links, but this is pretty lame. Git needs an option to treat a soft link the same way gcc would: as a copy of what it's pointing to, rather than just a pointer. Maybe git could have a command which takes a list of soft links, and then treats them as copies of whatever they're pointing to.