Giter Club home page Giter Club logo

awesome-git-hooks's Introduction

Awesome Git Hooks
Awesome Git Hooks

Awesome Lists PRs welcome

Awesome Git Hooks

⚓ Easy-to-use git hooks for automating tasks during git workflows.

Git hooks are custom scripts you can use to automate tasks which are triggered before or after a git command is executed. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. This repo contains helpful resources as well as a variety of git hook scripts that can be easily customized to serve different purposes.

✔️ Nothing to install/download

✔️ Code is well-documented

✔️ Grab & go! Copy the code you want to use and paste into your .git/hooks folder

Contributions are always welcome! Please see our Contribution Guidelines. Also, if you don't find the script you want below, you can create a new issue to request it.

Contents

Git Hook Scripts

Note: The icon next to each script signifies what language it is written in.

icon language
Bash Icon bash
Python Icon python
Perl Icon perl

commit-msg

  • enforce-insert-issue-number - Make sure user did not delete the ISSUE-[#] string that was generated by prepare-commit-msg/insert-issue-number.hook. Python Icon

post-checkout

  • delete-pyc-files - Delete all .pyc files every time a new branch is checked out. Python Icon
  • new-branch-alert - Display a message when a new branch is checked out for the first time. Bash Icon

post-update

  • update-server-info - Prepare a packed repository for use over dumb transports (e.g. http). Bash Icon

pre-commit

  • dotenvx - Prevent committing your .env file(s) to code. Bash Icon
  • format-code - Run command to format code and re-add any files modified after formatting. Bash Icon
  • search-term - Fail commit if a specific term is found in the code. Bash Icon
  • spell-check-md-files - Check files with .md extension for spelling errors. Bash Icon
  • verify-name-and-email - Fail commit if user.name or user.email is incorrect. Bash Icon

prepare-commit-msg

pre-push

  • prevent-bad-push - Prevent push of commits where the log message starts with "WIP" (work in progress). Bash Icon

pre-rebase

  • prevent-rebase - Prevent topic branches that are already merged to 'next' branch from getting rebased, because allowing it would result in rebasing already published history. Bash Icon

query-watchman

  • fsmonitor-watchman - Output to stdout all files that have been modified since a given time. Perl Icon

update

  • update - Block unannotated tags from entering. Bash Icon

Quick Start

  1. Pick a hook, any hook! Try the "verify-name-and-email" one if you're not sure where to start.
  2. Navigate to your project's hooks folder (.git/hooks).
  3. You should see a list of files already in there. Create a new file called the exact commit type that you want to use (eg: "commit-msg", "pre-rebase", "pre-commit", etc). Do not give it an extension.

create new file

  1. Open your new file and paste the code from the hook you chose out of this repo (eg: verify-name-and-email.hook).
  2. Save file. Done! Now the git hook will be triggered automatically.

Tools

  • Husky - Manage git hooks with a nice user interface.

  • Overcommit - A fully configurable and extendable git hook manager.

  • Git Build Hook Maven Plugin - Install Git hooks and config during a Maven build.

  • CaptainHook - Git hooks manager for PHP developers.

  • pre-commit - A framework for managing and maintaining multi-language pre-commit hooks.

Written Guides

Video Guides

License

CC0
This work is licensed under a Creative Commons Attribution 1.0 International License.

awesome-git-hooks's People

Contributors

anarcroth avatar anemortalkid avatar annarailton avatar cmmata avatar compscilauren avatar julio-b avatar knpwrs avatar motdotla avatar rudikershaw avatar saik0s avatar zackse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

awesome-git-hooks's Issues

New hook / script proposals

Is your feature request related to a problem? Please describe.
When people want to use git hooks to check something on the project, send notifications, or any kind of action, they have to know a bit of bash scripting or any other console language like python.

Describe the solution you'd like
It could be interesting to accept script proposals, like "I have a project X and would like to have a pre-commit script that lints every file commited with their respective linter". Then someone with enough knowledge (and time) could write that script and add it to the repo. Proposals could be Github issues for example, but it should be nice to add something like "if you don't find the script you want, you can ask for it in a new issue" in the Readme.

Add a new hook

Is your feature request related to a problem? Please describe.
We are always on the lookout for new Git hooks to add to this awesome list! It can be any kind of Git hook as long as it is ethical and appropriate. In order to qualify as being "awesome", it should be useful in some way and it should follow the Contribution Guidelines.

Describe the solution you'd like
You can contribute a new Git hook that is either:

  1. original and written by you
  2. a script you found somewhere, but it must be okay to copy and you must cite the source

Once you are ready to contribute a Git hook, feel free to create a new pull request! Also feel free to post any questions below. Happy coding!

new-branch-alert hook shows message when checking out current branch

Description of Bug
If the user tries to checkout the branch they are currently on, the "new-branch-alert" hook is activated. It should not be activated in this case.

Steps to Reproduce
Steps to reproduce the behavior:

  1. In terminal, type git checkout [current-branch-name]
  2. See error

Expected Behavior
The new-branch-alert message should not appear in this scenario.

Screenshots
bug-report-screenshot

verify-name-and-email test pollutes local git config

Description of Bug
After running the verify-name-and-email unit tests, the local git config is unconditionally updated with a hardcoded user.name and user.email.

Steps to Reproduce
Steps to reproduce the behavior:

  1. Run bats tests/verify-name-and-email-test.bats
  2. Run git config --local user.name; git config --local user.email
  3. Observe name/email in output

Expected behavior
Any existing local config for user.name and user.email should be preserved. If there isn't a local configuration for those fields, they should remain unset.

$ git config --local --remove-section user

# note: no output (fields are unset)
$ git config --local user.name
$ git config --local user.email

$ bats tests/verify-name-and-email-test.bats
 ✓ incorrect name throws error
 ✓ incorrect email throws error

2 tests, 0 failures

# note new values
$ git config --local user.name
Lauren Stephenson
$ git config --local user.email
[email protected]

Identify script language

Is your feature request related to a problem? Please describe.
The readme has a list of scripts separated by git hook, but I can't see what language the script depends on without opening it.

Describe the solution you'd like
It could be nice to identify the language of the script (maybe a small icon next to the title), so people can see the requirements. For example, if we have a server that does not have perl installed, maybe we can't use a perl script.

new-branch-alert.hook does not fire when the branchname start with a number

Description of Bug
A clear and concise description of what the bug is.
Hi when creating a new branch if it's start with a number (like new default Gitlab recommended branch naming convention) {issuenumber}-{title} ex : 15-styleborder the new-branch-alert.hook will not fire

Steps to Reproduce
Steps to reproduce the behavior:

  1. Create a branch '15-styleborder the hook will not fire'
  2. Now create a branch 'styleborder-15 the hook will fire as expected

Expected behavior
the new-branch-alert.hook 'should fire' whatever naming format used for the new branch

Thanks

Sample for stopping commit if user name or email is incorrect

Problem

When working with multiple projects, sometimes you want to use a different name or email address for the projects. If you don't use the correct ones, it can cause the commit history on the repository to look different than what was intended.

Solution

Create a sample that, upon enabling, will abort the commit if the user name or email is incorrect.

format-code hook tries to re-add deleted files

The "format-code" git hook will fail to commit if one of the added changes is the deletion of a file.

The current work-around is to run the formatting command manually, then add "--no-verify" to the end of the git commit command.

Add new-branch-update-maven-submodules hook

The "new-branch-alert" hook is the only current git hook to help solve the problem of checking out a new branch and forgetting to update the submodules to reflect the new branch name. This "new-branch-update-maven-submodules" git hook would automatically update the submodules when a new branch is created, then commit the code and display an informative message in the console.

Command: mvn clean -N versions:update-child-modules -DgenerateBackupPoms=false
Look into how to make it fully automatic rather than making user change the parent version prior to running above command.

Add line of code to pre-commit run command sample

Problem

After mvn fmt:format is run, it can potentially modify the code. So it needs to be re-added before committing.

Solution

Add line to end of file: git diff --name-only --cached | xargs -l git add

Also, clarify that the commit format run command will re-add any files that were modified after being staged. However, if a file was not staged before running commit command, it will still not be added after code is reformatted by format command.

Add Tools for Working with Git Hooks section

It would be beneficial to add a new section on the readme containing a list of useful tools that can be used to improve user experience/management of git hooks. The new section could be called "Tools for Working with Git Hooks" or something similar.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.