12

Is it possible to tell Git to ignore symlinks ? I'm working with a mixed Linux / Windows environment and, as you know, symlinks are handled very differently between the two.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Andrei
  • 1,606
  • 4
  • 19
  • 31
  • 3
    What exactly do you mean by ignore? You can just add the symlinks to your .gitignore if that is what you want. – CB Bailey Aug 18 '10 at 10:58
  • Yes but if just add the filenames it would also ignore the files that are symlinked...although I guess I could specify the full paths. I was hoping for something I could stick in a config file... – Andrei Aug 18 '10 at 11:05
  • 3
    Your best bet might be to generate the list with a `find * -type l`, I guess. – Cascabel Aug 18 '10 at 12:47
  • Hey, good idea :). I was hoping there was something built-in though. – Andrei Aug 18 '10 at 12:56
  • 4
    I don't understand. Either the files that are symlinked are already in your git working tree, in which case they will be tracked where they are, or they are not, in which case you are not tracking them anyway. If you want to work with filesystems that don't support symlinks then surely you should not use symlinks rather than just ignoring them? – CB Bailey Aug 18 '10 at 13:20
  • 1
    Maybe this will make things clearer: I'm doing a lot of `git add .` in order to track new files, counting on my `.gitignore` to exclude files I don't want to be tracked. Of course I can just add each symlink manually to the `.gitignore`, which is what I'm doing right now, just thought there might be some built-in way to prevent symlinks from being tracked. – Andrei Aug 24 '10 at 19:23
  • What are your symlinks linking to/why are you looking to ignore them? – blueberryfields Oct 13 '10 at 22:18
  • I'm taking a stab here - you might want to set your core.symlinks setting to "false" on windows, and stop ignoring the links. They'll get added to your repository, and then checked out on windows as innocuous text files. This seems to be the best automated solution available to your problem (other than shell scripts you run before committing to update your ignore list) – blueberryfields Oct 13 '10 at 22:21
  • This question might provide the desired answer: http://stackoverflow.com/questions/5917249/git-symlinks-in-windows – Maze Jul 28 '11 at 12:46

2 Answers2

2

No, it is not possible to do this globally. However, if you have lots of symlinks here is a bash script that you can use to easily add them to your repo's .gitignore file:

for f in $(git status --porcelain | grep '^??' | sed 's/^?? //'); do
    test -L "$f" && echo $f >> .gitignore; # add symlinks
    test -d "$f" && echo $f\* >> .gitignore; # add new directories as well
done
ColinM
  • 13,367
  • 3
  • 42
  • 49
2

Use git version >= 1.6

Git used to treat sym-links the same as regular files, but newer git versions (>= 1.6) check if a file is beyond a symbolic link and will throw a fatal error.

e.g.:

# git init 
# mkdir newdir 
# touch newdir/foo 
# git add newdir/foo 
# git commit -m 'add foo' 
# mv newdir /tmp/ 
# ln -s /tmp/newdir 
# touch newdir/bar 
# git add newdir/bar 
fatal: 'newdir/bar' is beyond a symbolic link

# git add/tmp/newdir
fatal: '/tmp/newdir' is outside repository

# git --version
git version 1.7.3.4
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • 3
    git 1.7.9.5 adds symlinks just fine, even when they point to something outside the repository... – Izkata Jun 24 '14 at 03:23