This a step by step tutorial, teaching you how to leverage git to deploy your website to your remote server. It will guide you through each and every step. Familiarity with [Git](https://git-scm.com) and the [Linux Shell](http://linuxcommand.org/lc3_learning_the_shell.php) are a plus, but not mandatory. ### THE WORK #### 1\. Login to server Open your terminal and login to your server using the following command: ssh your_user@server_ip_address * replace _server\_ip\_address_ with the actual [IP address](https://en.wikipedia.org/wiki/IP_address) of your server. * replace _your\_user_ with the actual username. By default, the username is the same as the host machine, unless you specify a different agent. Insert your password, and voilà… you are now logged to your server. #### 2\. Installing GIT To install git on your server, copy the following commands one at a time into the terminal and hit `Return`. If prompted, insert your password. sudo apt-get update sudo apt-get install git #### 3\. Create a folder for your code to go into The source code to your website needs to be put somewhere. By convention, code goes inside the `/var/www` directory. Navigate there using: cd /var/www Now, create a new folder to put the source code. For this tutorial, it will be called _website\_folder._ mkdir website\_folder Now, the full path to where you will put your source code is `/var/www/website_folder/`. It is important to remember this path because you will need it when setting up your git repository. #### 4\. Initialise a git repository on your server The git repository needs a folder to host it. A good practice is to call that folder website\_name.git. Inside of `/var`, create a folder called _repo,_ which will contain your git repositories. One of which will be _website.git_. To do so, run the following command: mkdir -p /var/repo/website.git Now, navigate to `/var/repo/website.git` and initialize your git repository: cd /var/repo/website.git/ git init --bare Voilà… now you have a [bare](http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/) repository for sharing. #### 5\. Create Hook > A [Hook](https://git-scm.com/docs/githooks) is a program you can place in a hooks directory to trigger actions at certain points in git’s execution. Git has several hooks that it can call after different stages automatically. You will use the _post-receive_ hook which is called after your repository has received pushed code. After initialising your git repository, new folders should appear inside `/var/repo/website.git/`. Navigate to _hooks_ and create a new [bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) called _post-receive_ using your preferred text editor. cd hooks nano post-receive Inside of the newly created file, you need to tell git where to put the files pushed. To do so, paste the following code: #!/bin/sh git --work-tree=_path\_to\_website\_folder_ --git-dir=_path\_to\_git\_directory_ checkout -f name\_of\_branch * replace _path\_to\_website\_folder_ with the correct path `/var/www/website_folder` which we created in point 3. * replace _path\_to\_git\_directory_ with the path to the bare git repository, which in this case is `/var/repo/website.git`. * name\_of\_branch is an optional parameter. If not specified, it defaults to `master`. You can follow that script with any other commands of your choice, like restarting the server for instance. Quit the editor using `ctrl+x`, and make sure to save the file as _post-receive_. #### 6\. Make the script executable In order for the operating system to execute the script, the latter needs to have executable permissions. To do so run the following command: chmod +x post-receive The work on your server is done. You can now logout by simply running `logout` and hitting `Return` #### 7\. Push local code to the server From your terminal, navigate to your local folder, and if it is not already a working git repository, initialise it as one. Then in order to configure git to push code to the remote server, you need to point it to its address. git init git remote add _name\_of\_repository_ ssh://your\_user@`server_ip_address/`_path\_to\_git\_directory_ * _name\_of\_repository_ can be any name you want. It is the name of the remote repository. * replace _server\_ip\_address_ with the actual [IP address](https://en.wikipedia.org/wiki/IP_address) of your server. * replace _your\_user_ with the actual username. By default, the username is [root](https://en.wikipedia.org/wiki/Root_directory). * replace _path\_to\_git\_directory_ with the path to the bare git repository, which in this case is `var/repo/website.git/`. Now, to push code to your remote server just run from inside your local git repository the following command: git push _name\_of\_repository name\_of\_branch_ Enter your password, and now your code is live and should be found inside of `/var/www/website_folder`