{"id":58269,"date":"2020-11-28T14:41:00","date_gmt":"2020-11-28T20:41:00","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=58269"},"modified":"2020-11-28T14:41:00","modified_gmt":"2020-11-28T20:41:00","slug":"hosting-remote-git-repositories-with-cpanel","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/hosting-remote-git-repositories-with-cpanel\/","title":{"rendered":"Hosting Remote Git™ Repositories with cPanel"},"content":{"rendered":"\n
Version control systems are the foundation on which modern development workflows are built. They reduce the complexity of collaboration while empowering developers to track and control the evolution of source code. Git is the most widely used open-source version control system, and that\u2019s why cPanel & WHM makes it easy to host Git repositories and automatically deploy code from repositories to production. <\/p>\n\n\n\n
In this article, we introduce Git and show you how to create and use remote Git repositories hosted on your cPanel server. We\u2019ll also explain cPanel Git Version Control\u2019s<\/em> killer feature: automatic code deployment. <\/p>\n\n\n\n Git is a version control system created by Linus Torvalds, who is also the lead developer of the Linux kernel. Version control systems track changes in source code and help multiple developers to work efficiently on the same codebase. With Git, developers can manage their code, safely merge contributions from many sources, and revert code to an earlier version if something goes wrong. <\/p>\n\n\n\n A software project\u2019s Git repository is often hosted on an external server. Individual developers download a copy to their laptop or desktop computer, make changes, and then upload their new version to the server. As you can imagine, coordinating changes from many different developers is enormously complicated. Git aims to make that process, if not simple, then at least manageable. <\/p>\n\n\n\n Here are a few key terms to help you understand how Git works before we get to setting up a repository and managing code. <\/p>\n\n\n\n We\u2019ve omitted some technical details from these definitions, but if you’d like to learn more, the Official Git Glossary<\/a> is a valuable source of information. <\/p>\n\n\n\n Git is a flexible tool that can be used in many different ways, but we\u2019ll focus on a simple workflow that hosts and manages a Git repository with cPanel. We\u2019re not going to discuss branches in this article, but there will be some resources at the end if you want to broaden your understanding. <\/p>\n\n\n\n We\u2019ll explain how to:<\/p>\n\n\n\n After reading this article, you will be able to host your site\u2019s code in a remote version control system while writing code on your local laptop or desktop computer. <\/p>\n\n\n\n Before we begin, you\u2019ll need:<\/p>\n\n\n\n To push code to the server\u2019s Git repository, you will also need to create an SSH key pair and install the public key on your server. We\u2019ll show you how to do that first. <\/p>\n\n\n\n We\u2019ll use SSH to communicate securely with our Git repositories and SSH keys, a convenient alternative to passwords, to authenticate with the server. <\/p>\n\n\n\n SSH keys come in pairs, a public key and a private key. The private key is kept on your local development machine. The public key is uploaded to the server. You can only authenticate with SSH on the server if you have the private key that matches the uploaded public key. <\/p>\n\n\n\n We\u2019ll generate the key pair on our local development machine with OpenSSH and use cPanel\u2019s SSH Access<\/em> tool to upload the public key. OpenSSH should be installed already on macOS and Linux, and you can follow this documentation<\/a> to install it on Microsoft Windows.<\/p>\n\n\n\n Open your terminal and run the following command:<\/p>\n\n\n\n <\/p>\n\n\n\n Follow the instructions to select a name for the key pair and a passphrase. If you choose to enter a passphrase, make sure you remember it. You won\u2019t be able to use the keys without it. <\/p>\n\n\n\n Next, open the directory you created the key pair in, where you\u2019ll find a couple of files. We\u2019re interested in the one called id_rsa.pub. <\/em>Open it in a text editor and copy the contents.<\/p>\n\n\n\n Navigate to the SSH Access <\/em>page in cPanel, which is in the main page menu\u2019s Security section.<\/p>\n\n\n\n Click Manage SSH Keys <\/em>and then Import Key.\u00a0<\/em><\/p>\n\n\n\n Choose a name for the key and paste the public key into the bottom text area. Then click Import.\u00a0<\/em><\/p>\n\n\n\n Now we\u2019re set-up with SSH keys, we\u2019ll be able to clone, push, and pull code with Git. But first, we need a repository. <\/p>\n\n\n\n The remote repository will be the canonical location of the site\u2019s code during development. People who work on the code will download it from here and upload their changes, which Git will merge with the existing code. <\/p>\n\n\n\n Use the File Manager <\/em>to create a new directory on your server to store the site\u2019s development files. Do not use spaces in the directory\u2019s name.\u00a0<\/p>\n\n\n\n Open cPanel\u2019s Git Version Control<\/em> interface.\u00a0 To use the remote repository on our local dev machine, we must first clone it to create a local repository. Cloning creates a complete copy. Open a terminal on your local device and use cd <\/em>to move to a directory you would like to store the project\u2019s files in. <\/p>\n\n\n\n Then, run the following command, replacing the username, domain, and location with your remote repository and server details.<\/p>\n\n\n\n Git may warn you that you have cloned an empty repository. We\u2019ll fix that in the next section, but before we do, let\u2019s look at how we can sync changes in the remote repository to our local environment. <\/p>\n\n\n\n We don\u2019t want to clone the server\u2019s repository every time something changes. We\u2019re only interested in the differences between the server\u2019s copy and our local copy. To only get what\u2019s changed, we use the pull command. Git commands are run from inside the project directory. <\/p>\n\n\n\n In this command, \u201corigin\u201d means the remote repository, the one on our server. It\u2019s a good idea to pull from the remote whenever you start a new development session so that you always have the most up-to-date version of the code. <\/p>\n\n\n\n Let\u2019s create a file on our local machine and push it up to the server. For demonstration, we\u2019ll create a simple index.html <\/em>file. Open your preferred text editor, paste the following HTML, and save it in the project directory.<\/p>\n\n\n\n First, we have to stage the new file, which tells Git that we want to include it in the repository. <\/p>\n\n\n\n Next, we commit our changes. <\/p>\n\n\n\n Git will ask you to enter a short description of your work with the default terminal text editor. Write a message, save the file, and close the editor. If your default editor is Vim or Vi, press \u201ci\u201d to enter insert mode, type your message, press escape, then type \u201c:wq\u201d, followed by the enter key. <\/p>\n\n\n\n Now all that\u2019s left is to push our local repository up to the remote:<\/p>\n\n\n\n This sends our changes and merges them with the remote repository. If anything has changed on the remote since you last pulled, Git will refuse to push. You\u2019ll have to pull again to make sure that you have the latest version. <\/p>\n\n\n\n If you take a look in the project directory in cPanel\u2019s File Manager, <\/em>you will see the HTML file you created is now on your server. <\/p>\n\n\n\n Once you\u2019ve finished editing your new site, you\u2019ll want to deploy it to production. You could manually copy the site\u2019s files to public<\/em>_html<\/em>, but cPanel Git Version Control<\/em> can automate the deployment process. <\/p>\n\n\n\n In your local project folder, create a file called .cpanel.yml. <\/em>This file tells cPanel which code to deploy and where to put it. In the simplest case, it looks like this. <\/p>\n\n\n\n —<\/p>\n\n\n\n deployment:<\/p>\n\n\n\n tasks:<\/p>\n\n\n\n Here we\u2019re copying our index.html<\/em> file into public_html<\/em>. It\u2019s unlikely that your deployment process is that simple, and you can learn more about the deployment file format in Guide to Git – Deployment<\/a>. <\/p>\n\n\n\n Save the file, and push it to the remote repository. <\/p>\n\n\n\n The period (.) tells Git to add all unstaged changes. Then we commit and push as before. <\/p>\n\n\n\n With the .cpanel.yml <\/em>file in place, cPanel automatically deploys changes when they are pushed from local development machines to the server. If you would prefer to choose when the code is deployed, you can use a pull deployment method described in our Deployment documentation<\/a>. <\/p>\n\n\n\n You can also manually deploy code at any time. Open Git Version Control<\/em> in cPanel and click the Pull or Deploy <\/em>tab. At the bottom of the page, there is a button called Deploy Head Commit. <\/em>Clicking it deploys your latest code as described in the YAML configuration file.\u00a0<\/p>\n\n\n\n This tab also displays information about the last successful deployment and the commit that kicked it off.<\/p>\n\n\n\n The workflow we\u2019ve outlined here is perfect for simple Git repository hosting, local development, and automatic deployment for sites and apps managed by your cPanel account. However, Git is a powerful and complex tool that supports many different workflows. To learn more about Git and version control, take a look at these resources. <\/p>\n\n\n\n As always, if you have any feedback or comments, please let us know. We are here to help in the best ways we can. You\u2019ll find us on Discord<\/a>, the cPanel forums<\/a>, and Reddit<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" Version control systems are the foundation on which modern development workflows are built. They reduce the complexity of collaboration while empowering developers to track and control the evolution of source code. Git is the most widely used open-source version control system, and that\u2019s why cPanel & WHM makes it easy to host Git repositories and […]<\/p>\n","protected":false},"author":77,"featured_media":65593,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-58269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\nWhat is Git?<\/strong><\/h2>\n\n\n\n
Hosting Git Repositories with cPanel<\/strong><\/h2>\n\n\n\n
Prerequisites<\/strong><\/h3>\n\n\n\n
<\/figure>\n\n\n\n
Create and Upload SSH Keys for Git<\/strong><\/h3>\n\n\n\n
ssh-keygen<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
Create a New Git Repository on Your Server<\/strong><\/h3>\n\n\n\n
<\/figure>\n\n\n\n
Click the Create <\/em>button and enter the location of the project directory. You can also give the repository a name. Make sure the Clone a Repository <\/em>switch is not activated. When you\u2019re finished, click Create<\/em> at the bottom of the page.\u00a0<\/p>\n\n\n\n<\/figure>\n\n\n\n
Clone the Repository to a Local Development Machine<\/strong><\/h3>\n\n\n\n
git clone ssh:\/\/user1@example.com\/home\/user1\/NewSiteDev<\/code><\/pre>\n\n\n\n
git pull origin<\/code><\/pre>\n\n\n\n
Push Local Changes Back to Your Server<\/strong><\/h3>\n\n\n\n
<!DOCTYPE html>\n<html>\n<head>\n<title>New Site<\/title>\n<\/head>\n<body>\n<h1>New Site Placeholder Page<\/h1>\n<p>We'll use Git to push this file to our remote repository.<\/p>\n<\/body>\n<\/html><\/code><\/pre>\n\n\n\n
git add index.html<\/code><\/pre>\n\n\n\n
git commit<\/code><\/pre>\n\n\n\n
git push origin<\/code><\/pre>\n\n\n\n
Automatically Deploy Code to Production with cPanel<\/strong><\/h3>\n\n\n\n
- export DEPLOYPATH=\/home\/user\/public_html\/\n- \/bin\/cp index.html $DEPLOYPATH<\/code><\/pre>\n\n\n\n
git add . <\/code><\/pre>\n\n\n\n
git commit<\/code><\/pre>\n\n\n\n
git push origin<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Streamlined Version Control and Deployment with cPanel<\/strong><\/h2>\n\n\n\n