Skip to content

Instantly share code, notes, and snippets.

@cameronmalek
Last active February 4, 2019 18:37
Show Gist options
  • Save cameronmalek/4353675 to your computer and use it in GitHub Desktop.
Save cameronmalek/4353675 to your computer and use it in GitHub Desktop.
The `hooks/pre-receive` file I use for cameronmalek.com and other sites. It makes sure the working directory is clean before pushing updates, forcing the developer to commit and pull server-side updates first. It doesn't detect any staged, uncommitted files. A situation where someone stages but fails to commit a file isn't anticipated.
#!/bin/sh
export GIT_WORK_TREE=/home/cameronm/public_html/
export GIT_DIR=/home/cameronm/git/cameronmalek.git
ec=0
# test for unstaged, uncommitted files
git diff --exit-code &>-
if [ "$?" -ne 0 ]; then
echo "Changes not staged"
ec=$(($ec + 1));
fi
# test for unadded files
if [ -z $(git ls-files --exclude-standard --others) ]; then
exit 0;
else
echo "Unadded files"
ec=$(($ec + 1));
fi
exit $ec;
@jrfnl
Copy link

jrfnl commented Feb 13, 2014

Very nice and just what I was looking for ;-)

Would be interesting to see the "staged, but not committed files"-check added as well for completeness
``git diff --staged`

Another idea I'm playing with is to get the script to commit any changes before exiting and stopping the push, so the repo is "pull-ready" for the developer to merge the changes.
What are your thoughts on that ?

@cameronmalek
Copy link
Author

Very nice and just what I was looking for ;-)

Would be interesting to see the "staged, but not committed files"-check added as well for completeness
``git diff --staged`

Another idea I'm playing with is to get the script to commit any changes before exiting and stopping the push, so the repo is "pull-ready" for the developer to merge the changes.
What are your thoughts on that ?

Wow, 5 years later and I finally see this comment for the first time. These days I don't need a script like this since DevOps came around.

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