<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Blog by Xeon Zolt]]></title><description><![CDATA[on random Thoughts, Stories, ideas and tech stuff]]></description><link>https://blog.xeonzolt.com/</link><image><url>https://blog.xeonzolt.com/favicon.png</url><title>Blog by Xeon Zolt</title><link>https://blog.xeonzolt.com/</link></image><generator>Ghost 4.7</generator><lastBuildDate>Mon, 08 Jun 2026 23:53:57 GMT</lastBuildDate><atom:link href="https://blog.xeonzolt.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Git Hooks]]></title><description><![CDATA[<h3 id="tl-dr">tl-dr</h3><blockquote>python Django project git hooks tutorial for automating certain tasks and explanation about git hooks</blockquote><h2 id="what-are-git-hooks">What are git hooks</h2><ul><li>Git hooks are scripts/programs that Git executes before or after a particular event occurs in a Git repository. Git hooks are a built-in feature - no need to download</li></ul>]]></description><link>https://blog.xeonzolt.com/git-hooks/</link><guid isPermaLink="false">60c5f5525b74e6c9d910cb92</guid><dc:creator><![CDATA[Xeon Zolt]]></dc:creator><pubDate>Sun, 13 Jun 2021 12:10:24 GMT</pubDate><content:encoded><![CDATA[<h3 id="tl-dr">tl-dr</h3><blockquote>python Django project git hooks tutorial for automating certain tasks and explanation about git hooks</blockquote><h2 id="what-are-git-hooks">What are git hooks</h2><ul><li>Git hooks are scripts/programs that Git executes before or after a particular event occurs in a Git repository. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally.</li><li>they are placed in hooks directory to trigger actions at certain points in git&#x2019;s execution. Hooks that don&#x2019;t have the executable bit set are ignored.</li><li>They let you customize Git&#x2019;s internal behavior and trigger customizable actions at key points in the development life cycle. for example I want to spell-check for commit message ( I have spellophobia ) or I want to check code for PEP8 maybe before commit or I want to run migrations after taking pull if one is available its only limited to what you can imagine and trust me it will make you fast and efficient also little lazy</li></ul><h2 id="how-do-i-implement-git-hooks">How do I implement Git hooks?</h2><p>The short and easy: Overwrite (or create) one of the scripts in .git/hooks and make it executable.</p><pre><code class="language-bash">chmod +x .git/hooks/scipt
</code></pre><h2 id="git-hooks-for-improving-python-django-project-flow-if-you-read-it-it-can-be-used-for-other-projects-too">Git hooks for improving Python Django project Flow ( if you read it it can be used for other projects too )</h2><h3 id="pre-push">pre-push</h3><p>it is called after <code>git push</code> after it has checked the remote status, but before anything has been pushed. If this script exits with a non-zero status nothing will be pushed</p><p>we will start by creating a file in hooks folder</p><pre><code class="language-bash">touch .git/hooks/pre-push
</code></pre><p>now lets edit the file and lets create a check that no one can push to master</p><pre><code class="language-bash">#!/usr/bin/env bash
branch=&quot;$(git rev-parse --abberev-ref HEAD)&quot;

if [&quot;$branch&quot; = &quot;master&quot;]; then
	echo &quot;you cannot commit to &apos;&quot;$(branch)&quot;&apos; branch&quot;
	exit 1
fi
</code></pre><p>now final step make it executable</p><pre><code class="language-bash">chmod +x .git/hooks/pre-push
</code></pre><p>now whenever you will push to master it will fail it and throw you a message</p><h3 id="post-merge-runs-aftergit-pull">post-merge ( runs after<code>git pull</code>)</h3><p>we will start by creating a file in hooks called post-merge. so we can do many things after a pull request like if its a node project we can check for changes in package.json and run npm install similarly for python project we can check for requirement.txt file.</p><p>I have a new idea to not run migrations for a Django project after taking pull let it run automatically and let me forget that such a command exists.</p><pre><code class="language-bash">#!/usr/bin/env bash

# lets check for changed files
changed_files = &quot;$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)&quot;

# function to check and run if we see file we want
check_and_run() {
	echo &quot;$changed_files&quot; | grep -i --quiet &quot;$1&quot; &amp;&amp; eval &quot;$2&quot;
}

# using for python packages
check_and_run migration &quot;python $(git rev-parse --show-toplevel)/manage.py migrate&quot;

# using for checking packages and its update
check_and_run requirements.txt &quot;pip install -r requirements.txt&quot;

</code></pre><p>&#x200B; replace python and pip with the path of your virtual env</p><h3 id="pre-commit-this-is-one-of-the-most-useful-hook-to-make-sure-you-are-not-doing-any-mistake">pre-commit ( this is one of the most useful hook to make sure you are not doing any mistake )</h3><p>we are going to use a already available framework for this <a href="https://pre-commit.com/">pre-commit</a> but you can also make a file too.</p><ol><li>install pre-commit: <code>pip install pre-commit</code></li><li>Add <code>pre-commit</code> to <code>requirements.txt</code> (or <code>requirements-dev.txt</code>)</li></ol><p>Define <code>.pre-commit-config.yaml</code> with the hooks you want to include.</p><pre><code class="language-yaml">repos:
-   repo: https://github.com/ambv/black
    rev: stable
    hooks:
    - id: black
      language_version: python3.4
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.9
    hooks:
    - id: flake8
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.1.0  
    hooks:
    -   id: trailing-whitespace
</code></pre><p>flake is used for checking python file format you can create a config file too if you want it to be more specific</p><p>black will fix formating errors before commit</p><p>also you can find many more plug ins at <a href="https://github.com/pre-commit/pre-commit-hooks">pre-commit-hooks</a> git repo</p><ol><li>Execute <code>pre-commit install</code> to install git hooks in your <code>.git/</code> directory.</li></ol><h2 id="notes">Notes</h2><h3 id="q-i-don%E2%80%99t-want-to-use-hook-how-to-bypass-it-during-commit">Q: I don&#x2019;t want to use hook. How to bypass it during commit ?</h3><pre><code class="language-bash">git commit --no-verify
# or
git commit --n
</code></pre><h3 id="q-i-only-want-to-run-lint-on-diff">Q: I only want to run lint on diff ?</h3><p>you can try a package called <a href="https://pypi.org/project/lint_diffs/">lint-diffs</a></p><h2 id="references">References</h2><p><a href="https://githooks.com/">https://githooks.com/</a> <a href="https://www.git-scm.com/doc">https://www.git-scm.com/doc</a> <a href="https://github.com/aitemr/awesome-git-hooks">https://github.com/aitemr/awesome-git-hooks</a></p>]]></content:encoded></item></channel></rss>