Adding Git version control to web sites

3.00 avg. rating (63% score) - 5 votes

Here’s a little script I made to make it easy to put a number of existing web sites residing under /var/www under version control by Git. Regarding security, it is obviously not a good idea to initialize the Git repository directly under the site directory, since from there it might be served by the web server to the outside world. And that is not what I want. Instead what I want is to have the Git working directory directly in the site directory that is served, and the repository somewhere else. That is perfectly possible with Git, just by setting one or two environment variables.

What this script does is actually create an interactive subshell for working with the separated working directory and repository.  The environment is initialized automatically, the umask is set correctly, and the prompt is changed to remind the user she is working inside this particular site’s environment. You can deploy it many times for multiple sites, customize it for each site, and keep all the repositories under one root directory, each in their own subdirectory. You don’t have to remember any of this, just the name of the site or script. You are ready to work with the site once you simply run the script. Pull from a remote, make changes, commit, push, whatever you want. When you are finished, just type ‘exit’ to get out of the site’s environment.  You can then run another script to work on another site.

So for example, if you have two sites, called first.example.org and second.example.org, save two copies of the script on your server under /usr/local/bin or $HOME/bin. The name of the first copy could be first.example.org and the name of the second copy could be second.example.org. Edit both files and set the WORKROOT variable to where your sites are situated (eg. /var/www) and REPOROOT to where you want your repositories to go. These paths can be the same in both files. What must be different is the WORKNAME variable, which should be the name of the directory under which the particular site is. The variables should look something like this:

/usr/local/bin/first.example.org:

 /usr/local/bin/second.example.org:

This configuration means your site roots are:

  • /var/www/first.example.org
  • /var/www/second.example.org

Those will be used as the Git working directories. The corresponding repositories will be:

  • /var/git/www/repos/first.example.org
  • /var/git/www/repos/second.example.org.

But you don’t really have to remember that because all you need to do is run either of the scripts and just use git as you are used to. When you’re done, type ‘exit’ or hit Ctrl+D and you can run another script to get to another site’s environment.

Give yourself permission to execute the script files under /usr/local/bin.

Here’s what an example first run looks like. First create the repository root directory and set suitable permissions (assuming you belong to the admin group):

Then run the first script:

As you can see, there is no Git repository yet. That has to be initialized and a first commit made.

As you can see, this is just typical Git usage. You can configure remotes, push and pull, make changes and commit them.

The second time you enter the site’s environment by running the script, the welcome message will look a bit different.

After running the second.example.org script you can go and initialize the repository for the second site as well. It is just the same as the first one.

Below is the script. Just save it, and customize the WORKROOT and REPOROOT variables how you like them. Then make as many copies of the script as you have sites, and customize the WORKNAME variable to match each site. Add execute permissions.

It is quite straightforward. First the configuration variables are defined, as well as the RCFILE variable which is read from a here document. Next the important Git variables GIT_DIR and GIT_WORK_TREE are exported. After possibly creating directories, another bash is executed in interactived mode, and the RCFILE variable is fed to it as the rc file to be executed at the start of the shell. So in reality the user will be working in a sub-subshell until she exits.

 

One thought on “Adding Git version control to web sites”

  1. If you have trouble cloning a remote to a repository initialized by this script, after starting the shell first time, run:

    git init
    git remote add origin [url-used-to-clone]
    git pull origin master

    If you can’t get the master branch, use “git remote show origin” or “git ls-remote” to see which branches are available.

Leave a Reply to kortsi Cancel reply