Developing quality apps is a complex process. To be successful, we need to use tools that help handle coding. These processes often need automation. Due to the needs of programmers and sysadmins, this process reduces deployment time. Git is one of the most popular tools to fulfill this particular purpose. In this tutorial, let's learn about Git Hooks to increase the efficiency of a VPS project!
Git Hooks are internal utilities that can improve the way Git is used. Learn how it works exactly!
What is Git?
Before learning about Hooks, learn about Git.
Git is an open source version control application. With this application we can follow the development of the software in detail.
Git is very popular with programmers and most open source projects use the platform.
What are Git Hooks?
With Git, we can create project development branches, register changes, and control absolute versions. However, you can automate this process. Git automation can work at the programming and implementation levels. That's what Hook does.
Git hooks are shell scripts that run automatically before or after Git executes an important command like Commit or Push. For the Hook to work, it is necessary to give the Unix system execution permission. By using these scripts you can automate a number of things.
Git will trigger Git hooks examples as soon as the local repository starts up. It should be noted that sites like Github or Gitlab do not allow it to be used in the first instance. So they are mainly used in local or private instances
How to use Git Hooks
Hooks are stored in directory .git/hooks/ of each cloned project or in the newly created local repository. There we will find many examples of Hooks.
To activate the hook, just create the file and save it in the directory .git/hooks/. However, the filename has been assigned by Git according to Git's definition as below:
Git Hook | Git command | Description |
applypatch-msg.sample | git am | When the patch notice is changed |
commit-msg.sample | git commit | To set commit execution notification |
fsmonitor-watchman.sample | launch watchman | To integrate watchman |
post-update.sample | git push | By updating all data after push |
pre-applypatch.sample | git am | Before applying patch |
pre-commit.sample | git commit | Before committing |
prepare-commit-msg.sample | git commit | When the commit message is set |
pre-push.sample | git push | Before pushing |
pre-rebase.sample | git rebase | Before skipping or entering |
pre-receive.sample | git push | When we push and receive data from remote repository |
update.sample | git push | By updating remote data when push |
As we can see, every Hook carries a Git command. Thanks to it, we can calculate when it is convenient to use Hook.
Git Hooks Example
The uses of Hooks are wide, but knowledge of Bash and other languages like Python or Ruby is needed to be able to take advantage of its full potential. Here are some basic examples:
Display information about the Commit . implementation
This example shows information about the commit action. Create a file called prepare-commit-msg in the directory .git/hooks/ of the repository. Then write the following script:
#!/bin/sh
SOB=$(git config github.user)
grep -qs "^$SOB" "$1" || echo ". Cambio por @$SOB" >> "$1"
Then save and set the execute permission for the file.
:~$ chmod +x prepare-commit-msg
With this simple Hook, when we commit, we will get relevant information about it immediately.
Document creation when changes are uploaded
The pre-push hook allows documentation of the code if we have a generator. Every time we make a change, the document will be made automatically.
Create the pre-push in the same directory as before and add the script:
#!/bin/bash
doxygen Doxyfile
git add docs/
git commit -m "Update documentation ($(date +%F@%R))"
Save the file and finally set the execute permission.
:~$ chmod +x pre-push
Find and Fix Trailing Whitespace in Commit
This kind of pre-commit Git Hooks is pretty simple to do. Create a file called pre-commit and add the following:
#!/bin/bash -l
.git/hooks/pre-commit-master-no-no
if [[ $? == 1 ]]
then
exit 1
fi
.git/hooks/pre-commit-debugger
.git/hooks/pre-commit-trailing-spaces
.git/hooks/pre-commit-images
.git/hooks/pre-commit-pair
Now trailing whitespace will be found and fixed in all commits.
Conclude
Git is an important tool for programmers today. Git hooks are a good way to improve this tool even more! In this tutorial, we looked at the basics of automating processes using Git Hooks.
Scripting Git Hooks can be tricky if you don't have a solid knowledge base in a programming language, like bash.
But now that you know what Git Hooks are, you've also learned the basics of Git Hooks first. We recommend that you follow the official document and keep digging deeper!
Post a Comment
Post a Comment