6

I just realized that I forgot to add a push hook to my freshly checked out repository and experienced some unpleasant consequences of that. I noticed that I cannot git add .git/hooks/pre-push. Can I somehow add a message to myself that will be displayed once I clone the repository that would remind me about adding the pre-push script?

d33tah
  • 10,999
  • 13
  • 68
  • 158

2 Answers2

0

This question has information that may be useful for simulating git clone hook.

Specifically by tweaking post-checkout hook to create some hidden file if it doesn not exist and displaying the message. In future checks - file exists so no message.

Don't forget to add that file to .gitignore too.

Community
  • 1
  • 1
Zloj
  • 2,235
  • 2
  • 18
  • 28
0

You can achieve this by creating a default-pre-push-script, containing the note, in a git-template-directory.

Amongst others the template-directory contains the hook-templates which will be copied to new repository, everytime the $GIT_DIR is created. And this is done everytime a local git-repository is initialized either by git init or git clone.

Example

Although the documentation of git-init offers different options to define a git-template, i will create a custom template-directory in this example.

1. Create a copy of the system-wide templates-directory

First of all we copy recursively a default template-directory to ~/.git-templates/ (you can name the directory however you want):

cp -r /Library/Developer/CommandLineTools/usr/share/git-core/templates ~/.git-templates

I'm working with a Mac - so i found a (there were multiple directories) origin-template-directory at /Library/Developer/CommandLineTools/usr/share/git-core/templates, a Linux-user should be able to find the system-wide templates at /usr/share/git-core/templates.

2. Create a default-pre-push hook-script

Put a script like the following in the ~/.git-templates/hooks, name it pre-push and make it executable:

#!/bin/sh

printf "\033[0;31m Note to myself:\033[0m don't forget to replace the pre-push-hook. :)\n"

exit 1 # this hook should always fail

3. Refer to the new template-directory in your ~/.gitconfig

Replace or add the init.templatedir-option, to make it refer on your new template-directory:

[init]
    templatedir = ~/.git-templates/

4. Test your new default-pre-push-hook

Everytime you're now initializing or cloning a git-repository you should get the defined note.

Please consider that the content of the origin template-dir may be change in the future, due to new releases of Git. In that case you might need to update the contents of your template-directory to eliminate nasty error-messages.

Florian Neumann
  • 5,587
  • 1
  • 39
  • 48