Git Hooks: We’re Not Using Them Enough!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you something that not many developers seem to be using which is gith hooks but be careful if you use them wrong gith hooks are more harmful than good more about that later if you want to learn how to design a new piece of software from scratch I have a free guide for you you can get it at ar. codesign guide it contains the seven steps I take when I design new software and hopefully it helps you avoid some of the mistakes I made in the past iron. c/d design guide the link is also in description of this video now what are GI hooks GI hooks are scripts that allow git to execute custom script before or after events such as committing or merging there are different types of hooks as well typically their server side and client side hooks server side hooks get triggered on network operations like pulling or pushing a repo client site is more for events like committing and merging local branches so let's take a look at a CL side hook called pre-commit and how you could use that so the first thing I'm going to do is create a folder my repo and then let's initiate a test repos story so now we have a directory with Git initiated inside of it GI hooks are located in the hidden dogit directory specifically in the folder hooks here you can see we already have some sample hooks by default these hooks are disabled to enable one of these examples we simply remove do sample from the name and by the way if you have initiated the git repository like I did for this example without using a special type of git template then you will most likely have some sample hooks stored there so let's now focus on the naming of these files as you can see the names have either post pre as a prefix and sometimes it has a suffix like message this naming tells us when the hook is going to be executed for example pre-commit is going to be executed before the commit has been made and post commits after the commit has been done now let's create a python script that's going to be executed when we're trying to commit a change first let's create a file pre-commit dopi and then what you can do is update the pre-commit script to actually run this python file note that the dollar asteris that you see here is to propagate any arguments that we pass with commands like get checkout here's an example of what you could put in a pre-commit python file this case we simply print the arguments and we then interrupt the action and now inside our repo let's create a file called read me like so so now we're going to add the read me file and then let's commit it and as you can see it now actually runs the python script and prints the argument so why should we actually use this I mean this python script is actually quite bad it just doesn't let us commit but that's not really the point the point is that we can run Python scripts when actions are occurring with Git and that means we can now upgrade our developer workflow to do all sorts of fun things like encouraging a commit policy or setting limits on what a commit actually can contain so I have a question for you have you ever used this before and if so what kind of gith hooks are you using and what do they do let me know in the comments there's one thing that you need to be really careful of specifically with pre commit hooks like the one that I've showed you just now if you're on a hook that's really complex and takes a long time maybe you're doing extensive linting or very comprehensive test that can significantly slow down the development process it's generally better to rely on continuous integration systems for these task so that if you're working on your code a very long pre-commit Hook is not going to slow you down continuous integration tasks can be created with get up actions if you want learn more about how to set up a c ICD Pipeline with G up actions check out this video next thanks for watching and see you next time
Info
Channel: ArjanCodes
Views: 12,712
Rating: undefined out of 5
Keywords: git hooks, git hooks python, git hook, git hooks example, git hooks deploy, python pre-commit hooks, pre-commit hooks, pre-commit hook git, git pre commit hook python, git hooks post commit, pre-commit python, how to use git hooks, git hooks pre-commit, git hooks pre-commit example, pre-commit hook, github tutorial, github, python github, python programming, python programming tutorial, tuesday tips, arjancodes github, arjancodes, arjan python
Id: r_le07f_rfQ
Channel Id: undefined
Length: 4min 6sec (246 seconds)
Published: Tue May 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.