Setting up Git on WebFaction
I recently setup some web hosting with WebFaction for a client. While developing the website locally I have been using git as my version control system, because I’m hip and trendy.
Since my development machine is a laptop (and often behind a NAT router), it would be easier for me to push to the server rather than pulling them since the server’s details will remain constant while mine are likely to change frequently. While merely setting up git on WebFaction is simple, there are a couple of gotchas if you wish to be able to push to the server.
While I have tried to make each step as clear as possible, I realise that I’m not very good at making clear instructions. This is not a git tutorial either so it may perhaps be a bit confusing to those with no prior experience of using it.
Installing Git
- Login to your server via SSH
- Download and install git (check the git website to make sure you’re downloading the latest version):
$ wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz $ tar zxf git-1.6.3.3.tar.gz $ cd git-1.6.3.3 $ ./configure --prefix=$HOME && make && make install
Setup the remote repository
With git installed on the server, it’s now time to setup the remote repository. Usually when you want to initialise a repository git init would suffice. Unfortunately that won’t do here. Thankfully, it’s not too difficult if you follow these steps.
- Create a folder for your repository. I tend to have a
repofolder in my home directory which contains a folder for each project:$ mkdir ~/repo/projectname.git
The .git suffix is just a style thing and not required.
cdinto this newly created directory and initialise the remote repository like so:$ git --bare init
Those of you with prior git experience may wonder what the
--bareis for. This tells git that we don’t need copies of the committed files on the filesystem, just the repository itself. Apologies for the rather vague description but it should become clear later on.
Configuring the local repository
After working on the server configuration, it’s now time to do a little work on your local machine. At the moment, your local git repository doesn’t know where to push the files to. This will soon change!
-
$ cd /path/to/your/local/repository/ $ git remote add origin user@server-ip-or-address:~/repo/projectname.git
Of course, make sure to substitute the italic phrases for your own details
Now that your local repo has a reference to the remote one, it’s time to give this pushing malarky a try!
-
$ git push origin master
- You’ll be prompted for your ssh password but after that everything should be hunky dory. Let’s look at the output:
$ bash: git-receive-pack: command not found $ fatal: The remote end hung up unexpectedly
Uh oh. That’s not good.
Don’t despair! We can fix this
So what’s happened? Well after googling the problem, I came across this blog post which states:
Many installations of sshd do not invoke your shell as the login shell when you directly run programs; what this means is that if your login shell is bash, only .bashrc is read and not .bash_profile. As a workaround, make sure .bashrc sets up $PATH so that you can run git-receive-pack program.
What this basically means is that when you run git push, the server looks for a program called git-receive-pack but doesn’t know where to find it. Thankfully this is easy to fix.
- On your server, switch to your home directory and use your preferred text editor (e.g. emacs, vim, or nano) to open
.bashrc - Don’t worry if there’s stuff in there, just add
export PATH=${PATH}:~/binto the bottom. - Back on your local machine, try pushing once more. This time your output should look something like:
Counting objects: 250, done. Compressing objects: 100% (228/228), done. Writing objects: 100% (250/250), 453.36 KiB, done. Total 250 (delta 116), reused 0 (delta 0) To [removed to keep away from prying eyes] * [new branch] master -> master
Now to setup a working directory
Wait, what? We’re not finished yet? I’m afraid so. There is just one more step to do. Remember before I mentioned --bare? Well this means that the files in the repository cannot be accessed directly. Not being able to access the files isn’t terribly useful if you’re wanting to deploy them onto your server. Thankfully making them accessible is easy to do.
- Switch to the directory on the server you wish to deploy the files. Since I’ve been working on a django project, this would be
$ cd ~/webapps/django $ git clone ~/repo/projectname.git- Now you should have a working directory with all of your lovely code
In conclusion
Hopefully this is useful for those who are using WebFaction shared hosting as I know a number of people have been looking at setting up git and getting push to work. Possibly people on other shared hosting accounts may find it useful as well.
Happy git pushing!
cool! helped me a lot
aha, thank you!
Thank you! I just came across the git-upload-pack: command not found error myself.
Thanks for posting this. Big help.
Very solid tutorial.
Would I need to then do a ‘git pull’ in my app folder every time I pushed from my local machine?
Yes, you would do a
git pullas you would essentially have two repositories on the webfaction server: one you had pushed the changes to, and one containing the working copy.To be honest, if you did
git initinstead ofgit --bare initthen you would be pushing the changes to a repository with a working copy (so you could also skip the Now to setup a working directory section) sogit checkoutwould suffice. I haven’t tested that as when I set it up,git --bare initwas useful to what I was doing. It might not be necessary in most cases though. I shall investigate and update the post if that’s the case.Hello, Tom. I’m Daniel, a technical writer at WebFaction. This is an excellent tutorial! It’s also the approach I’d recommend on our system. It’s my plan to officially document pushing to git repositories hosted on WebFaction soon, but it’s always nice to see WebFaction users helping out when something isn’t covered yet.
Pushing a local branch to a checked out remote branch won’t update the working tree on the remote repo. You’ll have to go in and do `git checkout` to update the working tree.
I’ve got a `src` folder in my web apps in which I do a `git init`. That will check out the `master` branch, which is where I’m comitting the code in my local repo that production-safe.
After I’m happy with everything, I do `git push` to my repo and run `git checkout` to get the latest code out on the server.
Hello,
Thank you for your tutorial, I’m starting to pursue my indie dev side project passions and this will help tremendously! I appreciate it.
-Joseph