Skip to content

Instantly share code, notes, and snippets.

@jaymzcd
Created September 22, 2011 09:42
Show Gist options
  • Save jaymzcd/1234427 to your computer and use it in GitHub Desktop.
Save jaymzcd/1234427 to your computer and use it in GitHub Desktop.
Few git things for pushing remotes/setting up on centos/rhel

Git things internally

Installation of Git on RHEL/CentOS

first, and you probably wont really ever need this unless you're doing a bare bones install of a cent/rhel box, the webtactic repo maintains a decently up to date git install that will cleaning go onto such a box:

sudo rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm
sudo yum install --enablerepo=webtatic git-all

Now you have working git!

Something to remember about hooks

When writing hooks there's a few nuances with the directories git lands you in. You can either cd hard to the folder or just cd .. if you trust it. Also, unset the GIT_DIR variable or you can run your command prefixed with env -i to tell the shell process to ignore the environment variables (that's probably better!).

Adding a checked out copy as pushable remote

Second thing... we've often got a working copy checked out on the remote server. First off, remember to copy over your .gitignore & .gitconfig files to your remote home. Now checkout your code to wherever you are and then do the following if you want to be able to push to it and have it all update (yes, I realise there are also some drawbacks with this method, but for present use-case it's making life easier):

git config receive.denyCurrentBranch ignore

now add a post-receive hook (a file in .git/hooks/post-receive) and put in:

#!/bin/sh
cd ..
env -i git reset --hard

as you can see, it jumps out of the .git folder and then does a hard reset, this occurs after it's recieved a push so the two combined will let the working copy essentially update itself.

Now i can add this working copy as a remote aliased to production and push to it for it to all update in one:

git remote add production ssh://udox@footlocker.eu/home/live/newsletter/
git push production ← yay!

handy for many of our working deployments and easy/quick to setup. To disable you can just remove that hook or switch off the branch ignore.

Automatically running a commit sql file against the db

post-commit for local OR post-receive remote

This looks at the commits coming in and if it finds a sql file in the list of changed files then it'll import that. You'll need to change the user/password/database of course. It's only grabbing the top one (we typically are only using this against a particular dump anyway). You could probably cast that sql_file command output to an array and then iterate over it and call mysql to import them. No time for me to do that yet though!

#!/bin/bash
echo "Checking for new sql file (only first one!)";
sql_file=`env -i git log --name-only -1 --oneline --pretty="format:" | sort -u | grep '\.sql' | head -1`;
echo "Importing $sql_file";
env -i mysql -u'root' -p'!PASSWORD' DATABASE < $sql_file;
echo "Complete";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment