1

I want to remove trailing line whitespace from certain files and I found this slick script that supposedly does that -- https://makandracards.com/makandra/11541-how-to-not-leave-trailing-whitespace-using-your-editor-or-git .

It advises to "append the following code to the pre-commit hook template".

However, I don't know where that is.
Where do I find/create such a template?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Dave
  • 15,639
  • 133
  • 442
  • 830
  • 3
    Look in the git folder. More especially in the '.git/hooks' folder. – Philippe Nov 25 '19 at 23:17
  • I'd like to further @Philippe's comment in that it was NOT apparent to me that the path isn't off of `~/`, like `~/.ssh`, but rather per-project, so it took awhile for me to finally find what I needed was `~/workspace/myproject/.git/hooks`. Hope that saves somebody time. – Neil Gaetano Lindberg Feb 11 '22 at 20:02

1 Answers1

2

All Git repository should be created following a template which includes a .git/hooks/hooks--pre-commit.sample

Rename that file in hooks--pre-commit, make sire it is executable (unless you are on Windows), and that hook will be triggered automatically on commits, with the code you will have put in int.

Your article actually refers to a global hook:

The global git hooks live in $PREFIX/share/git-core/templates/hooks (where $PREFIX can be /usr/local/git, from whoami - fakeFaceTrueSoul's comment).
Rename the pre-commit.sample file to pre-commit to activate it.

On Windows, that would be in:

c:\gits\current\mingw64\share\git-core\templates\hooks>l
total 41K
-rwxr-xr-x 1 dchaffiol 1049089 1.6K Nov  6 14:43 pre-commit.sample*

On Mac, $PREFIX would be $(brew --prefix git) or just brew --prefix, as seen here.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. What would the $PREFIX be for Mac (I'm using Mac Mojave) – Dave Nov 26 '19 at 15:10
  • @Dave possibly the output of `brew --prefix git` – VonC Nov 26 '19 at 15:11
  • @Dave After answering your bounty (https://stackoverflow.com/a/59208885/6309), can you tell me if this answer (to your previous question "Where on my file system do I find a git pre-commit hook template?") was satisfactory, of is there anything missing to it? – VonC Dec 06 '19 at 07:41
  • JFYI, I found mine at `/usr/local/git/share/git-core/templates/hooks`, So `$PREFIX` could be `/usr/local/git` – whoami - fakeFaceTrueSoul Oct 24 '22 at 20:10
  • 1
    @whoami-fakeFaceTrueSoul Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Oct 24 '22 at 20:42