Git advanced Hooks

Start by imagining the following scenarios

  • Watching a colleague submit information, how can you tell what that submission is doing?
  • Pull colleague’s latest code, find compilation failure, delay their own development how to do?
  • What if I forget to trigger CI (Continuous Integration) after COMMITTING code?

The answers to these questions are simple: Ask a colleague and write a checklist of your own to make sure you follow the steps.

At low frequencies this scheme may work, but at high frequencies it is a bit cumbersome. Are you devastated that you can’t live or die locally because you forgot to deploy and the testers mentioned a bug? Is there an automated solution to this problem? Failure to compile will not allow the commit, commit will automatically run CI deployment.

The answer to the above question is git-hooks.

What is a Git – Hooks

Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. 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. You can use these hooks for all sorts of reasons.
Copy the code

This is a description of Git-hooks on Git. In simple terms, Git can trigger custom scripts when certain important actions occur, and these scripts are triggered by git-hooks.

Hooks are usually placed under the.git/ Hooks directory, and have corresponding examples that end with.sample.

Commit – MSG, which is used by Hooks

From the name you should know what this hooks do. In commit-msg, we can create a custom script to check for commits and reject those that don’t meet the team’s specifications. In this way, any colleague who sees the submission information can clearly define the scope of the submission.

Hooks 用法之 pre-push

Pre-push is triggered during a Git push run, in which you can perform project compilation, run unit testing, etc., to ensure that your push code doesn’t interfere with the rest of your colleagues’ development.

Post-receive, which is used in Hooks

Post-receive is a server-side hook that is triggered when the server receives some client-side push. In a post-receive, we can notify the server of the deployment of new code, or we can send an email to a specific person.

conclusion

Or maybe you think that this section is just extra work for the developer, or sometimes it’s just trivial stuff. If you can’t read the submitted information, look at the code directly. Compile failed to fix yourself quickly; CI forgot to deploy, go deploy yourself immediately; They seem to be small things, not worth mentioning. But software development is a team effort, and one failed build may cause everyone else to waste time trying to fix the problem. If there is a serious problem, it will block the development progress of the entire team.

Finally, as a qualified developer, you are responsible for your code. But there are times when you want to be lazy, and that’s where this script comes in. The script meticulously executes the preconfigured commands.