Use Git Hooks to automatically run some annoying but necessary tasks

Tram Ho

Original article Use Git Hooks to Automate Necessary but Annoying Tasks

Some tasks really need to be run after pulling the code or checking out to a branch such as updating dependencies or migrate database, re-index ctags to improve the experience better when developing applications. Besides, developers often forget to do it, leading to errors. To identify the recent problem there is a set of git hooks with dotfiles that automate those tasks.

Git Hooks

Git has a commonly used feature hooks . You can think of a hook as an event that activates before and after some state during modification control. The hooks to pay attention include:

  • prepare-commit-msg – Fire before commit message.
  • pre-commit – Fire before a git commit .
  • post-commit – Fire after a git commit .
  • post-checkout – Fire after changing branches.
  • post-merge – Fire after merging branches.
  • pre-push – Fire before code pushed to a remote.

Expanded Hooks

The Dotfiles convention for extension is a place to customize the hooks in the {pre,post}-$EVENT in the {~/.git_template.local/hooks} . At this point, whatever we add to the hook files will automatically execute the tasks that we often forget.

Tasks are often forgotten

Forget running re-index ctags !

The following lines of code will re-index for you after each git command

Forget running bundle install after checkingout to another branch

Automatically install new gems

Forget running the pending migrations

Automatically run migrations

Write API document with fdoc but forget to generate pages

Automatically generate HTML docs

Forget a Go commitment to the standard code format for the files

Run go fmt before commit

In there you can learn more about the Hookup gem for Rails including the Bundler hook versions and migrations.

We can focus on what’s more important by simplifying and automating small parts of the development process.

Thanks for reading my writing.

References

Share the news now

Source : Viblo