Putting Sites In Git

For years, I have version controlled my websites, first using CVS and then subversion. As part of my masters thesis, I am moving to git for versioning.

This requires figuring out how to handle my basic use case of allowing me to pull parts of the site and edit them from various locations on the net and push changes to the web server for publication.

The first step is relatively simple, creating a git repository on my computer at home:

mkdir gittest && cd gittest && git init

Making a copy of a repository is generally done using the clone command, however it will only pull over a network, so the following won’t work:

git clone . ssh://wjholcomb@madstones.com:gittest

Instead, an repository needs to be created on the remote machine through a separate channel:

ssh wjholcomb@madstones.com 'mkdir gittest && cd gittest && git init'

Now I can push to that repository:

git push ssh://wjholcomb@madstones.com/~/gittest/

For simplicity’s sake, I can add a remote to store that repository:

git remote add webserver ssh://wjholcomb@madstones.com/~/gittest/

Note pushes could be simply written as git push if the special name origin had been used. Using the remote name webserver, the command is now:

git push webserver

Pushing changes adds them to the remote object store, but it does not update the working directory. To update the working directory, I can do:

ssh wjholcomb@madstones.com 'cd gittest && git checkout -f'

The checkout -f command will destroy any local changes, however. It would be better to store the various commits and merge them. In order to do this, I will add the commits in a branch on the webserver:

git push webserver master:refs/remotes/ebene/master

Now, post commit, a merge can be done using:

ssh wjholcomb@madstones.com 'cd gittest && git merge ebene/master'

This merge process can be automated using git’s post-receive hook on the server:

ssh wjholcomb@madstones.com 'echo "read oldrev newrev refname
cd ..
env -i git merge \$refname" >> gittest/.git/hooks/post-receive
chmod a+x gittest/.git/hooks/post-receive'

Finally, to handle working from different machines, I added the following to my .bashrc:

function publish() {
branch=$(git branch 2> /dev/null | sed -e 's|.* ||')
[ -z "$branch" ] && { echo "Error: No git branch found"; return; }
remote=$(git remote)
[ -z "$remote" ] && { echo "Error: No git remote found"; return; }
echo "Publishing $branch to $remote"
git push $remote "$branch:refs/remotes/$(hostname -s)/$branch"
}

  • Share/Bookmark

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

CommentLuv Enabled