Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git hook to run a command after `git pull` if a specified file was changed.In this example it's used to run `npm install` if package.json changed and `bower install` if `bower.json` changed.Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
#!/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
# Example usage
# In this example it's used to run `npm install` if package.json changed
check_run package.json "npm install"
@yangg
Copy link

yangg commented Sep 4, 2014

Failed to run in windows, fix it by add a ! sign , #!/usr/bin/env bash

@robertcasanova
Copy link

robertcasanova commented Apr 27, 2015

This hook doesn't work if you run pull with --rebase option. Do you know how to fix it? Thx!

@danielcgold
Copy link

danielcgold commented Apr 29, 2015

I forked this to work with changes to .scss and .sass file changes. https://gist.github.com/danielcgold/6de270807fc01c793c9a

@mariusGundersen
Copy link

mariusGundersen commented May 13, 2015

It seems like HEAD@{1} should be used instead of ORIG_HEAD. Forked here: https://gist.github.com/mariusGundersen/8a4383f74a46ababc1de

@aaronroberson
Copy link

aaronroberson commented Jun 3, 2015

@robertcasanova take a look at .git/hooks/pre-rebase.sample and see if you can deduct how to do a post-rebase hook.

@gtramontina
Copy link

gtramontina commented Jun 18, 2015

@MoOx (and anyone interested): If you're using npm : https://github.com/gtramontina/ghooks

@DimeZilla
Copy link

DimeZilla commented Aug 13, 2015

@sindresorhus this is awesome! thank you!

@Satys
Copy link

Satys commented Aug 18, 2015

Does post-merge run when there are conflicts during pull?

@slattery
Copy link

slattery commented Aug 19, 2015

@Satys.

No, it won't run. "This hook cannot affect the outcome of git merge and is not executed, if the merge failed due to conflicts."

@punkeel
Copy link

punkeel commented Sep 4, 2015

❤️

@icamys
Copy link

icamys commented Apr 14, 2016

I would add these checks too:

# Updating git submodules
check_run .gitmodules "git submodule init && git submodule update"

# Installing composer dependencies
check_run composer.json "composer install"

# For those who use gulp
check_run web/assets "gulp --production"

@wzpan
Copy link

wzpan commented Apr 18, 2016

@robertcasanova

This hook doesn't work if you run pull with --rebase option. Do you know how to fix it? Thx!

+1 , same problem. Have you found out the solution?

@aaronroberson

take a look at .git/hooks/pre-rebase.sample and see if you can deduct how to do a post-rebase hook.

I've tried to write a post-rebase hook but it didn't work ...

@tkalfigo
Copy link

tkalfigo commented May 20, 2016

First line missing a !; should be #!/usr/bin/env bash

@belveryin
Copy link

belveryin commented May 20, 2016

post-rewrite works for me. As side effect it is also executed on amend.

http://stackoverflow.com/questions/21307744/git-post-rebase-hook

@jplagostena
Copy link

jplagostena commented Jun 14, 2016

Awesome! Thanks! It works like a charm.

@marcvangend
Copy link

marcvangend commented Jul 18, 2016

For package managers that use a lock file, eg. Composer, it would make sense to check if the lock file changed rather than the requirements file, right? In other words:

# Installing composer dependencies
check_run composer.lock "composer install"

@betorobson
Copy link

betorobson commented Feb 20, 2017

Thank you so much, it works like a charm although, I did a few changes on my fork.
https://gist.github.com/betorobson/23e5914b51e844bac5eaa6032d6f3f88

@johntholland
Copy link

johntholland commented May 9, 2017

On my machine it is running npm install whether package.json is changed or not.

The way I am reproducing is by creating a new branch at the current commit and then checking it out. Therefore the files should be identical between the two branches.

Anyone have any ideas?

cross-posted with stackoverflow: http://stackoverflow.com/questions/43876375/optional-githook-behaving-as-non-optional

@rlafuente
Copy link

rlafuente commented May 23, 2017

For cases where you are pushing to a bare repo and want to use this on a post-receive hook, instead of the line

changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

one should use

changed_files="$(git diff --name-only HEAD^ HEAD)"

Thank you for an extremely useful snippet!

@anoobbava
Copy link

anoobbava commented Nov 17, 2017

This only deals with changed files right. Suppose I want to check any new files are generated when taking pull, how this solution works?

@oriy
Copy link

oriy commented Aug 10, 2018

thanks for the wonderful solution
extended the script - added the option for conditionally execute the command according to git configuration
https://gist.github.com/oriy/e8b58a6a471d371129b1d0b430510375

@samuelj90
Copy link

samuelj90 commented Nov 22, 2018

@sindresorhus Thanks for the wonderful solution.
I would like to clarify that Is it possible to check and run npm install only if the change is in npm dependency ?
I think that will save some more time.

@drAlberT
Copy link

drAlberT commented Feb 11, 2019

@robertcasanova

This hook doesn't work if you run pull with --rebase option. Do you know how to fix it? Thx!

+1 , same problem. Have you found out the solution?

@aaronroberson

take a look at .git/hooks/pre-rebase.sample and see if you can deduct how to do a post-rebase hook.

I've tried to write a post-rebase hook but it didn't work ...

post-rebase is not a valid git-hook

You have to implement a post-rewrite hook and check the first argument to be "rebase" (could be "amend" too as @belveryin already tasted).
See https://git-scm.com/docs/githooks#_post_rewrite

@matthew-dean
Copy link

matthew-dean commented Apr 23, 2019

How do you run npm install if one of two matching criteria is found? e.g. I want to run npm install if one of two files is modified, but don't want to run npm install twice.

@ykrytsyn
Copy link

ykrytsyn commented May 1, 2019

You should be able to run it as
check_run "file1\|file2" "npm install"

Probably, I also need to add '-e' parameter to the 'grep' function

@paulolorenzobasilio
Copy link

paulolorenzobasilio commented Aug 1, 2019

Thanks!

@devinrhode2
Copy link

devinrhode2 commented Nov 22, 2019

@vigneshwaran-natarajan
Copy link

vigneshwaran-natarajan commented Mar 22, 2020

is there any way to show message like this, before & after hooks execution for npm i?
checking for dependency update...

if there is any update going to happen
installing dependency..

if there is no changes to dependency
no new dependency!

@ajaykumar97
Copy link

ajaykumar97 commented Mar 6, 2021

This script works like a charm. Thanks for sharing.
Can someone please let me know what is happening inside the script? What does this script arguments mean?

@henaharon
Copy link

henaharon commented Nov 14, 2021

@ajaykumar97 can you help me implement it ? it looks like it is not running ;/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment