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 and the are a plus, but not mandatory. Git Linux Shell 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 with the actual of your server. server_ip_address IP address replace with the actual username. By default, the username is the same as the host machine, unless you specify a different agent. your_user 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 . If prompted, insert your password. Return sudo apt-get updatesudo 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 directory. Navigate there using: /var/www 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 . It is important to remember this path because you will need it when setting up your git repository. /var/www/website_folder/ 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 , create a folder called which will contain your git repositories. One of which will be . To do so, run the following command: /var repo, website.git mkdir -p /var/repo/website.git Now, navigate to and initialize your git repository: /var/repo/website.git cd /var/repo/website.git/git init --bare Voilà… now you have a repository for sharing. bare 5. Create Hook A is a program you can place in a hooks directory to trigger actions at certain points in git’s execution. Hook Git has several hooks that it can call after different stages automatically. You will use the hook which is called after your repository has received pushed code. post-receive After initialising your git repository, new folders should appear inside . Navigate to and create a new called using your preferred text editor. /var/repo/website.git/ hooks bash post-receive cd hooksnano 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= --git-dir= checkout -f name_of_branch path_to_website_folder path_to_git_directory replace with the correct path which we created in point 3. path_to_website_folder /var/www/website_folder replace with the path to the bare git repository, which in this case is . path_to_git_directory /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 , and make sure to save the file as . ctrl+x 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 and hitting logout 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 ssh://your_user@ name_of_repository server_ip_address/ path_to_git_directory can be any name you want. It is the name of the remote repository. name_of_repository replace with the actual of your server. server_ip_address IP address replace with the actual username. By default, the username is . your_user root replace with the path to the bare git repository, which in this case is . path_to_git_directory 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
Share Your Thoughts