Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic link to a hook in git

I wrote my own custom post-merge hook, now I added a "hooks" directory to my main project folder (since git doesn't track changes in .git/hooks), somewhere I read that I can make a symbolic link from hooks to .git/hooks so I don't have to copy the file from one folder to the other every time someone changes it so I tried:

ln -s -f hooks/post-merge .git/hooks/post-merge

But it doesn't seem to work, any ideas why? "ln hooks/post-merge .git/hooks/post-merge" works fine but making a hard link is the same as copyin I guess....

like image 600
Mateusz Dymczyk Avatar asked Jan 04 '11 10:01

Mateusz Dymczyk


People also ask

Can I add a symbolic link to Git?

Git can track symlinks as well as any other text files. After all, as the documentation says, a symbolic link is nothing but a file with special mode containing the path to the referenced file.

What is symbolic link in git?

A symbolic link is a special type of file whose contents are a string that is the pathname of another file, the file to which the link refers. (The contents of a symbolic link can be read using readlink(2).) So a symbolic link is one more file, just as a README.md or a Makefile .

Can I push git hooks to remote?

No. Hooks are per-repository and are never pushed.

Can a symbolic link point to a directory?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows.


2 Answers

you just used wrong path, it should be:

ln -s -f ../../hooks/post-merge .git/hooks/post-merge 
like image 104
Michal Čihař Avatar answered Sep 19 '22 15:09

Michal Čihař


While you can use symbolic links, you can also change the hooks folder for your project in your git settings with :

git config core.hooksPath hooks/ 

Which is local by default so it won't ruin git hooks for your other projects. It works for all hook in this repository, so it's especially useful if you have more than one hook.

If you already have custom hooks in .git/hooks/ that you do not want to share with your team you can add them in hooks/ and add a .gitignore so they're not shared.

like image 32
Pierre.Sassoulas Avatar answered Sep 21 '22 15:09

Pierre.Sassoulas