January 10, 2011
Some of my Git repos are very, very simple. The commits are all on master, no branches in play at all. And the workflow is just as simple as back in the bad old Subversion days: Pull from the repo, commit some stuff, push it.
A good example is the repo I use to store my Project Euler solutions. It is only used by myself and I always want to push after committing.
So I set out to create (and complete within 10 minutes) my least ambitious project so far: A bash script to add “git push” as a post-commit hook in the current Git repo. You can find it on Github, but the business part of it is right here:
#!/bin/sh HOOKS_FOLDER=.git/hooks POST_COMMIT=$HOOKS_FOLDER/post-commit if [ -d $HOOKS_FOLDER ]; then if [ -f $POST_COMMIT ]; then echo "Post commit hook already exits, please add 'git push' manually in .git/hooks/post-commit" exit -1 fi echo "git push" > $POST_COMMIT chmod 755 $POST_COMMIT exit 0 else echo "This command must be run in the root of a Git repository." exit -1 fi
Suggestions welcome for improving the script. I think this is the first multiline Bash script I ever wrote, so I expect it to be flawed.
Ah yes, and before you comment: I know that this is not the shiniest of Git workflows, I know that branches are cheap etc. I love all the cool features of Git but sometimes I just don’t need them.
However I do not know if Git already has this functionality built in via some configuration option?