Search code examples
gitwebpost-update

Bring Website Under Source Control using .Git


I would like some advice about how to setup Git for an existing live website on a production server (only hosting one site - no vHosts etc).

There are several examples of how to achieve this - but all refer to when you are starting a new project.

I'd like to replicate this scenario which describes how to setup a remote repository and create a post-update hook to copy files into the www directory upon checkout. This is the concept I can grasp (https://danielmiessler.com/study/git/#website)

However, I'm facing the scenario where there is live code on a webserver, I want to initialise Git on all the existing live files, and then develop on my local machine.

I'm sorry that I don't know enough about Git understand how to achieve the result described in Daniel's article from my starting position of having live code on a server in production.

My Config (CentOS 6/Nginx):

Git Repository (this should be in a separate location from www)

/home/username/repository/website.git

Post Update Hook File Location

/home/username/repository/website.git/hooks/post-update

Post Update Hook File Entry

GIT_WORK_TREE=/var/www/website git checkout -f

Website Directory

/var/www/website

I also have a ignore file

/var/www/website/.gitignore

Could someone kindly assist with Git commands. I can initialise /var/www/website/.git (with an initial commit) but don't know how to then setup /user/username/repository/website.git to work in the way described in the article.


Solution

  • The steps shown in the guide are exactly the same for a new project or and existing one. The examples it gives in step 2 of creating an html file can be replaced by adding existing content into your working directory.

    So, run git init in your new working folder, although this can be done in an existing folder as it doesn't change any of the current files. Put your current web content into the same folder, either importing from a backup or pull down straight from your live environment it doesn't really matter.

    Now you need to run git add command for all the files you want under version control. You don't need to do them individually, wildcards and directories are accepted. Use git status to check that everything you want is staged and then git commit -m with some suitable message.

    I would suggest that you create another directory locally and issue a git init --bare command there, set up the hook to push to another directory and then push to the bare repo with

    git push --set-upstream  [/your/local/bare/repo] master
    

    Just to check that you have all the content and that you are comfortable with git