Skip to content

Instantly share code, notes, and snippets.

Forked from kylefrost/
Created Jun 25, 2014
What would you like to do?

Creating the post-commit file

Note: If you want to use your personal Twitter account to post commits, go to Step 2

  1. Create a new Twitter account for your commit messages. Example
  2. Go to and Sign In with your Twitter account you are posting commit messages to.
  3. Hover over your username in the top-right corner after signing in and select "My Applications"
  4. Create a new application
  5. The name, description, and site can all be whatever you want, but leave Callback URL empty
  6. Under "Application Settings" click "modify app permissions" next to "Access level"
  7. Select "Read and Write" and then click "Update Settings"
  8. On the tabs at the top, go to "API Keys"
  9. Scroll to the bottom and under "Your Access Token click "Create my access token"
  10. Copy down into a text document the "API Key" and "API Secret" under "Application Settings" as well as "Access Token" and "Access Token Secret" under the genereted "Your Access Token" - you need these later
  • You may have to wait a few moments and refresh in order to see your access token
  1. You can close Twitter Developer now
  2. Download oauth_sign and http_post, save them somewhere that you can easily find them
  3. Open your favorite Terminal application and cd to the directory of the uncompressed oauth_sign zip (e.g. cd /path/to/oauth_sign/)
  4. Run sudo make then sudo make install from inside the oauth_sign directory
  5. Now cd into the directory of the uncompressed http_post zip
  6. Run sudo make SSL_DEFS="-DUSE_SSL" SSL_LIBS="-lssl -lcrypto" then sudo make install
  7. Open a new Terminal window and make sure they are there by typing oauth_sign and http_post and it doesn't say command not found
  8. You can delete the left over files now
  9. Save the post-commit file from this gist and edit the file with your personal API Keys that you put in the text document earlier
  10. Copy post-commit into your local git repository in .git/hooks/post-commit
  11. Run chmod a+x on the post-commit in order to make it executable
  12. Now it should send a tweet every time you make a commit message from the repo you added it to

Add this file by default to every new git repo

  1. Create a .git_template/hooks/ folder in your home directory
  2. mkdir ~/.git_template
  3. mkdir ~/.git_template/hooks/
  4. Run git config --global init.templatedir '~/.git_template'
  5. Copy all the files from your current git repo's hooks folder (the one you put post-commit in) into the new hooks folder
  • e.g. cp -R /my/git/repo/.git/hooks/ ~/.git_template/hooks/
  1. Done! Every time you create a new repo on your machine, the post-commit file should be there
# PATH modification needed for http_post and oauth_sign
export PATH=$PATH:/usr/local/bin
toplevel_path=`git rev-parse --show-toplevel`
toplevel_dir=`basename "$toplevel_path"`
branch=`git rev-parse --abbrev-ref HEAD`
subject=`git log --pretty=format:%s -n1`
hashtags="#code #$toplevel_dir"
tweet=$hashtags' ['$branch']: "'$subject'"'
# truncate tweets that are longer than 140 characters
if [ ${#tweet} -gt 140 ]
tweet_trunc=$(echo $tweet | cut -c1-137)
consumer_key="YOUR API KEY"
consumer_secret="YOUR API SECRET"
access_token="YOUR ACCESS TOKEN"
http_post -h Authorization "$(oauth_sign \
$consumer_key $consumer_secret \
$access_token $access_secret \
POST "$url" status="$tweet")" \
"$url" status="$tweet"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment