Oaemirami
Oaemirami

Reputation: 27

How to test pre/post commit scripts in git?

I've created a git post-commit hook in python and made a file executable and placed it into .git/post-commit.

It works when I run it via Python. Now I want test it locally in git. How can I do that? I can't make a commit to git locally from my computer in an easy way, right?

Should I copy the hook file to my server and make test commits to test it? Is there another way?

Upvotes: 1

Views: 2126

Answers (2)

battlmonstr
battlmonstr

Reputation: 6300

pre-commit and post-commit hooks are run locally on commit (see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks ), not on push (push is what transfers your local commits to the server).

To test a hook I would create a test branch, and make several test commits there with whatever content you want. After the hook is ready, you can delete that branch. Until you do a push, everything is local.

Also if you output something from the hook script, that will be visible in the terminal when you do a commit. This is useful for debugging.

For executing the code on the server you should use "Server-Side Hooks" (see at the bottom of this page, also documented here).

Upvotes: 0

Mureinik
Mureinik

Reputation: 311978

I can't make a commit to git locally from my computer in an easy way, right?

You need to differentiate between pushing a branch to a server, which may require some configuration, and simply creating a commit, which can be as easy as running a command:

$ git commit --allow-empty -m "Testing my hook"

Upvotes: 2

Related Questions