Git

How to move your Githooks to your Repo

📣 Sponsor

If you're like me, you like to automate things when you push or pull them from a repo. Git Hooks are files which are run when you do an action, such as merge.

If you're also like me, you may have noticed that githooks are only relevant to that particular git location, as they are stored in the .git folder. You can however store your githooks in your repo, so they are available and run in every location, whether remote, local, or anything in between.

1. Create the folder

First of all, in your git repository, create a folder called .githooks. You can actually call it whatever you want, but I find .githooks makes the most sense.

2. Update Git

Next, update Git so that it uses that folder as your git repository. You can do this in Git 2.9 and above by using the following command:

git config core.hooksPath .githooks

If you do not have git 2.9 and above, you will have to update, since this functionality is not available before!

Once you've done that, your git repository will include your githooks in one folder, and will run them out of that folder. So if you put a post-merge file in you .githooks folder, it will run after you run the git merge command.

Now you can commit your git hooks to your repo, and other branches, allowing you to keep consistent versioning on your git automations.

Last Updated 1610483116016

More Tips and Tricks for Git

Subscribe for Weekly Dev Tips

Subscribe to our weekly newsletter, to stay up to date with our latest web development and software engineering posts via email. You can opt out at any time.

Not a valid email