2017-11-07 14:39:07 +11:00
---
id : precommit
title : Pre-commit Hook
---
You can use Prettier with a pre-commit tool. This can re-format your files that are marked as "staged" via `git add` before you commit.
## Option 1. [lint-staged](https://github.com/okonet/lint-staged)
2019-03-12 17:01:13 +01:00
**Use Case:** Useful for when you want to use other code quality tools along with Prettier (e.g. ESLint, Stylelint, etc.) or if you need support for partially staged files (`git add --patch` ).
2018-02-01 19:46:17 -08:00
2019-03-12 17:01:13 +01:00
_Make sure Prettier is installed and is in your `devDependencies` before you proceed._
2017-11-07 14:39:07 +11:00
```bash
2019-03-12 17:01:13 +01:00
npx mrm lint-staged
2017-11-07 14:39:07 +11:00
```
2019-03-12 17:01:13 +01:00
This will install [husky ](https://github.com/typicode/husky ) and [lint-staged ](https://github.com/okonet/lint-staged ), then add a configuration to the project’ s `package.json` that will automatically format supported files in a pre-commit hook.
2017-11-22 19:27:40 +08:00
2017-11-07 14:39:07 +11:00
See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
2018-01-09 02:19:53 +11:00
## Option 2. [pretty-quick](https://github.com/azz/pretty-quick)
2017-12-21 04:08:42 +08:00
2018-02-01 19:46:17 -08:00
**Use Case:** Great for when you want an entire file formatting on your changed/staged files.
2018-01-09 02:19:53 +11:00
Install it along with [husky ](https://github.com/typicode/husky ):
2017-12-21 04:08:42 +08:00
```bash
2018-09-24 07:31:50 +02:00
yarn add pretty-quick husky --dev
2017-12-21 04:08:42 +08:00
```
and add this config to your `package.json` :
```json
{
2018-07-04 15:15:46 +02:00
"husky" : {
2018-07-04 08:31:49 -07:00
"hooks" : {
2018-07-04 15:15:46 +02:00
"pre-commit" : "pretty-quick --staged"
}
2018-01-09 02:19:53 +11:00
}
2017-12-21 04:08:42 +08:00
}
```
2018-01-09 02:19:53 +11:00
Find more info from [here ](https://github.com/azz/pretty-quick ).
2017-12-21 04:08:42 +08:00
2018-03-02 07:14:20 -08:00
## Option 3. [pre-commit](https://github.com/pre-commit/pre-commit)
2017-11-07 14:39:07 +11:00
2018-02-01 19:46:17 -08:00
**Use Case:** Great when working with multi-language projects.
2017-11-07 14:39:07 +11:00
Copy the following config into your `.pre-commit-config.yaml` file:
```yaml
2018-07-29 15:44:21 +08:00
- repo : https://github.com/prettier/prettier
rev : "" # Use the sha or tag you want to point at
hooks :
- id : prettier
2017-11-07 14:39:07 +11:00
```
2018-03-02 04:26:36 -08:00
Find more info from [here ](https://pre-commit.com ).
2017-11-07 14:39:07 +11:00
2018-02-01 19:46:17 -08:00
## Option 4. [precise-commits](https://github.com/JamesHenry/precise-commits)
2018-10-07 13:42:41 +02:00
**Use Case:** Great for when you want partial file formatting on your changed/staged files.
2018-02-01 19:46:17 -08:00
Install it along with [husky ](https://github.com/typicode/husky ):
```bash
2018-09-24 07:31:50 +02:00
yarn add precise-commits husky --dev
2018-02-01 19:46:17 -08:00
```
and add this config to your `package.json` :
```json
{
2018-07-04 15:15:46 +02:00
"husky" : {
2018-07-04 08:31:49 -07:00
"hooks" : {
2018-07-04 15:15:46 +02:00
"pre-commit" : "precise-commits"
}
2018-02-01 19:46:17 -08:00
}
}
```
**Note:** This is currently the only tool that will format only staged lines rather than the entire file. See more information [here ](https://github.com/JamesHenry/precise-commits#why-precise-commits )
Read more about this tool [here ](https://github.com/JamesHenry/precise-commits#2-precommit-hook ).
## Option 5. bash script
2017-11-07 14:39:07 +11:00
Alternately you can save this script as `.git/hooks/pre-commit` and give it execute permission:
```bash
#!/bin/sh
2019-01-08 07:30:20 +01:00
FILES = $( git diff --cached --name-only --diff-filter= ACM "*.js" "*.jsx" | sed 's| |\\ |g' )
[ -z " $FILES " ] && exit 0
2017-11-07 14:39:07 +11:00
2019-01-08 07:30:20 +01:00
# Prettify all selected files
echo " $FILES " | xargs ./node_modules/.bin/prettier --write
2017-11-07 14:39:07 +11:00
# Add back the modified/prettified files to staging
2019-01-08 07:30:20 +01:00
echo " $FILES " | xargs git add
2017-11-07 14:39:07 +11:00
exit 0
```
2018-02-24 10:12:25 +02:00
If git is reporting that your prettified files are still modified after committing, you may need to add a post-commit script to update git's index as described in [this issue ](https://github.com/prettier/prettier/issues/2978#issuecomment-334408427 ).
Add something like the following to `.git/hooks/post-commit` :
```bash
#!/bin/sh
git update-index -g
```