Skip to content

Instantly share code, notes, and snippets.

@foca
Created July 20, 2012 02:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foca/3148204 to your computer and use it in GitHub Desktop.
Save foca/3148204 to your computer and use it in GitHub Desktop.
Git hook to detect if a file changed after a checkout/merge/rebase and show the diff.
#!/usr/bin/env bash
#
# Install into:
# - .git/hooks/post-merge
# - .git/hooks/post-rewrite
# - .git/hooks/post-checkout
#
# And make sure all are executable.
#
# Then change the $file appropriately. Enjoy.
file=".env.sample"
non-sample-file() {
echo $1 | sed "s/.sample//g"
}
if [[ $(git diff HEAD@{1}..HEAD@{0} -- "${file}" | wc -l) -gt 0 ]]; then
echo
echo -e "======> The file \e[33;1m${file}\e[0m changed!"
echo
git diff --color HEAD@{1}..HEAD@{0} -- "${file}" | sed 's/^/ /' | tail -n+5
echo
echo -e " Make sure to update your \e[33;1m$(non-sample-file $file)\e[0m."
fi
@Sodj
Copy link

Sodj commented Jul 15, 2022

Thanks, I needed this 👍

@akbiggs
Copy link

akbiggs commented Aug 16, 2022

Super useful, thanks.

@tomi-font
Copy link

tomi-font commented Sep 28, 2023

Thanks! Here is an alternative I made specifically for the West manifest file: https://gist.github.com/tomi-font/e548c23c7d9a321d452dde9c4cc43fa4

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