On cPanel Git can be used in two ways:
- clone a remote repository (e.g. from GitHub) to cPanel
- create a repository on cPanel and use it in a remote location (e.g. your pc)
Here I will cover both ways and highlight some important steps that are not mentioned in cPanel docs. Before we start, here are some terms that I will be using in this guide:
git | an open-source, distributed version control system |
clone | create a local copy of some repository |
remote | a common repository on another site, e.g. GitHub |
gitweb | an interface that allows browsing a git repository using a web browser |
Create a Git Repository in cPanel
Q: Why would you create a Git repository in cPanel?
A: You can create a git repo from your WordPress files on cPanel, then edit them locally on your PC. When you’re finished, simply submit changes back to cpanel. This gives you a free staging copy of your website to work on locally.
How to create a Git Repository in cPanel
Step 1. From cPanel go to Files > Git
Step 2. Click on Create button
Step 3. By default, cPanel toggles the git clone option, but because We want to create a new repository We will toggle off the Clone a Repository button.
Step 4. Fill in the file path to the new repository. In this example, I will use the public_html folder where I have a WP site installed.
Step 5. Provide a name for the repository as it will appear in your cPanel interface, e.g. wp-site.
Step 6. Click on the Create button.
cPanel will create the repository and provide you with some helpful Git commands that you can use.
In this screen cPanel states commands for two cases:
- For an existing project
- To start a new project
But, if you like me already have files in the directory (WordPress files) and now want to download those files to your computer using git clone, you first have to run the following commands in cPanel terminal:
1. enter the repository
cd /home/domain/public_html
2. Set your email address
git config --global user.email "you@example.com"
3. Set your Name (will be shown in git history)
git config --global user.name "Your Name"
4. Aldo you have physical files in the git repository (public_html folder), git still doesn’t know that these files exist and if you now clone the repository on your PC you will get an error: “You appear to have cloned an empty repository.“
To include all files from this folder to Git, run the following command:
git add .; git commit
This will open a screen that will ask you to set a commitment message and show you all files that are going to be included in git:
5. Type the commit message, for example, initial commit, and then type :quit + enter to exit Vim editor.
Now all files have been added to Git and you can clone/pull the repo to your local computer.
git clone ssh://user@example.com:22/home/domain/public_html
NOTE: If your hosting provider has disabled password logins for SSH and only allows access with SSH keys, then you first need to register an SSH key first.
Clone a Git Repository in cPanel
Aside from creating a new repository, you can also clone another git repo from sites like giuthub.com
Q: Why would you clone a Git repository from GitHub to cPanel?
A: This is a quick way to test some repositories, for example, you could test my JS terminal in less than a minute.
How to Clone a Git Repository in cPanel
Step 1. From cPanel go to Files > Git
Step 2. Click on Create button
Step 3. Copy the URL into the Clone URL field
In this example I will use: https://github.com/stefanpejcic/terminal
Step 4. cPanel will suggest a path, but you can alter it if you want.
Step 5. Provide a name for the repository as it will appear in your cPanel interface, e.g. terminal.
Step 6. Click Create button
You will be redirected back to the Git™ Version Control page and after cloning is done you will see the newly cloned repository:
TIP: Gitweb, a Git web interface is available in cPanel. It allows browsing a git repository using a web browser.
To lunch Gitweb for your repository, simply click on History button next to it.
You will be redirected to another page where you can browse the repository
For more useful Git commands check my Git CheatSheet – a practical git tutorial for beginners, focusing on the commands that are used daily by every programmer.