104

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....

4
  • 26
    Because the symlink is resolved relative to its location. A symlink in .git/hooks/ that points to hooks/post-merge gets resolved to .git/hooks/hooks/post-merge, which does not exist. You want ln -s -f ../../hooks/post-merge .git/hooks/post-merge. Or make your life easier: ln -s -f ../hooks .git/hooks. Your problem has nothing to do with git. Jan 4, 2011 at 13:02
  • 1
    stackoverflow.com/questions/3462955/… and stackoverflow.com/questions/427207/… (and stackoverflow.com/questions/3703159/…) point out the fact that symlink can work.
    – VonC
    Jan 4, 2011 at 13:04
  • 1
    Correct me if I'm wrong, but a Symlink still has to be setup per workstation. The only thing that this saves, is copying it around manually or writing another command that copies the tracked hook file into .git/hooks.
    – adi518
    May 18, 2019 at 18:43
  • @adi518 Any solution for this as of today? Was reading from Atlassian: > "As an alternative, Git also provides a Template Directory mechanism that makes it easier to install hooks automatically. All of the files and directories contained in this template directory are copied into the .git directory every time you use git init or git clone." More details on Template Directories [here](git-scm.com/docs/git-init#_template_directory) But I still don't get how to make this work... Jan 26, 2022 at 18:25

6 Answers 6

182

you just used wrong path, it should be:

ln -s -f ../../hooks/post-merge .git/hooks/post-merge
6
  • 11
    I don't understand why I need to go up two directories to link a resource that lives in the folder I've cd'd into. Shouldn't it just be ln -s ./hooks/ ?
    – yurisich
    Aug 4, 2013 at 19:13
  • 50
    This. When git is evaluating the symlink, it apparently does so using .git/hooks as its working directory, so relative paths should be relative to that directory. This is more self-explanatory if you first cd into .git/hooks before making the symlink, and figure out the relative path from there.
    – Eliot
    Feb 6, 2014 at 19:57
  • 12
    @Eliot neither creation nor resolution of symlinks is affected by the working directory. Whatever you give ln will be stored as the target and resolved relative to the location of the link. Mar 17, 2016 at 1:18
  • 2
    @JoóÁdám You're right. So the issue here is that the original command specifies an incorrect relative path. Still, cding into .git/hooks before you make the link will help you write the command, as you can then autocomplete to the correct path.
    – Eliot
    Mar 30, 2016 at 20:01
  • 1
    All this worked for me in the end. The only difference is that I am linking to my own prepare-commit-msg. The problem is if I am editing the commit message using nano, then Ctl+X out to abort, git still completes a commit anyway instead of aborting like it used to before I made this change. Is there a way to have nano exit without causing this commit to complete?
    – Frak
    Jun 15, 2017 at 20:57
35

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.

4
  • 4
    Very nice! A handy trick :) Seems far more future-proof than sym-linking them one by one.
    – jkp
    Jun 6, 2019 at 14:03
  • 2
    this functionality is supported for git > 2.9 github.com/git/git/blob/master/Documentation/RelNotes/… Mar 4, 2021 at 11:09
  • 1
    How does this not have way more upvotes? This seems objectively better for future-proofing.
    – Farzad A
    Oct 27, 2021 at 14:51
  • I think this is the best solution, by far. The only caveat I see is that this precludes any private hooks one dev on a team has...but OP addresses that concern, and it is far outweighed by the benefits, IMO. Oct 26, 2022 at 20:20
3

Changing directory before linking

cd /path/to/project-repo/.git/hooks
ln -s -f ../../hooks/post-merge ./post-merge
1
  • Even simpler, after the cd: ln -s -f ../../hooks/post-merge
    – jamesdlin
    Jan 29, 2019 at 21:47
1

The path calculation is done relative to the symlink. Let's understand using an example,

ln -s path/to/file symlink/file

Here, the path to the file should actually be the relative path from the symlink path.
The system actually calculates the file path as symlink/path/path/to/file
The above command should be re-written as

ln -s ../path/to/file symlink/path

The folder structure being,

/code
------ symlink/file
------ path/to/file

0

Utilizing Michael Cihar's comment, here is an example of a bash script I wrote to simply create these symlinks. This script is located in git_hooks/ dir which is at the project root. My .git/ folder is also in the same directory level.

#!/usr/bin/env bash

pwd=$(pwd);

# Script is designed to be ran from git_hooks/ dir
if [[ "$pwd" == *"git_hooks"* ]]; then

  files=$(ls | grep -v -e '.*\.');

   while read -r file; do

     ln -s ../../git_hooks/$file ../.git/hooks/
     echo "Linked $file -> ../.git/hooks/$file"

   done <<< "$files";

else

  echo "";
  echo "ERROR: ";
  echo "You must be within the git_hooks/ dir to run this command";
  exit 1;

fi

My script must be ran from within the actual git_hooks/ directory. You can modify it to behave differently, if you'd like.

This script will symlink any file that is not suffixed with a file extension within the git_hooks/ directory. I have a README.txt in this directory + this script (named symlink.sh). All the actual git hooks are named 'pre-commit', 'pre-push', etc. so they will be symlinked.

-3

why not just cp ./hooks/* .git/hooks/

this worked for me in Mac OS

1
  • 18
    Because I don't have to copy the file from one folder to the other every time someone changes Oct 23, 2017 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.