Level up your development in Git
Tags: git, devops, development, beginners
Published: 2023-06-03 12:41:07

> As developers, we strive to improve ourselves every day and become better than we were before. I have found some tools and solutions that can help us. I would like to share a trick that I often use to simplify complex processes.

Today, we use GIT to **version control** in daily work. We have many styles in GIT workflow like feature branch, single branch, and other. The Git is concept and software that help us to saving and tracking file(code) history in standard idea. We use Git in different platforms such as GitHub, Gitlab, Bitbucket, and other. 

We have process in step to review and ensure our code source before we commit code and push them to the Git server. How can we ensure our source code that correct syntax or available to execute in another place? Therefore, we make an Unit-test and a linter(also linting framework) that we can execute to verify our code in local. Maybe, We have a separate environment that make to test our source code, up to you!.

To help us to accelerate in development feature. I prefer you to use a open source project from community that called [Pre-commit](https://github.com/pre-commit). We can use them to replace our manual step such as run unit-test and fix format. Then, how can we use it? I will show you in this blogs.

## What is pre-commit

It is a framework for managing and maintaining multi-language pre-commit hooks.

> Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically point out issues in code

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9910b573-8d9d-400c-8f43-bb4393894068/Untitled.png)

## Usage step by step
- Install into local machine

Before you can run hooks, you need to have the pre-commit package manager installed.

Using pip for Linux

```bash
# for Python2
pip install pre-commit

# for Python3
pip3 install pre-commit
```

Using `homebrew` for MacOS

```bash
brew install pre-commit
```

- Initial pre-commit policy in root directory source code

Add a pre-commit configuration. You must created file named `.pre-commit-config.yaml`. You can generate a very basic configuration using `pre-commit sample-config` .

```yaml
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
    -   id: check-yaml
    -   id: end-of-file-fixer
    -   id: trailing-whitespace
-   repo: https://github.com/psf/black
    rev: 22.10.0
    hooks:
    -   id: black
```

- Install Git hook script

If you want to automate execute **pre-commit** in every committed. You should install Git hook before.

```bash
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
```

now **pre-commit** will run automatically on git commit!

- Run against all files

```bash
$ pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/psf/black.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Check Yaml...............................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1

Files were modified by this hook. Additional output:

Fixing sample.py

black....................................................................Passed
```

oops! looks like I had some trailing whitespace. Now, you can fix your error first and re-add your code to commit stage in Git again.

## Conclusion

We can automate our development processes with framework called `pre-commit`. If you need to use `pre-commit` in advance step, you can read more details in below reference link and kindly share your idea to improve our process with us.

## Ref

1. https://pre-commit.com/#intro
2. https://github.com/pre-commit/pre-commit