In this article, we will be learning how to deploy a project with Git. The goal is to take a project under Git version control and deploy a live “production” version. This can be used to host scripts that are edited frequently or even to launch a static website (or the static files of a site).
This is a great option for anyone using Git as the version control application for their projects. These steps will also work for dedicated hosting services.
For housekeeping purposes, we will begin with some assumptions and requirements. We will assume that you already use Git regularly and are familiar with some of the basic commands. Before we begin, make sure you have a project in a local environment, and make sure the files do not contain sensitive information (like passwords). Otherwise, you’re ready to go live.
Setting up a Bare Git Repository on Your Server
To deploy our git project to the greater web, we will first log into our VPS/Dedicated server to prepare the environment to receive our
git push. This will become the location of a “remote repository”. You can have as many remote repositories as you want because each one will have an assigned name.
- Log into your VPS/Dedicated server via the shell
- Create a directory in your Home directory named whatever you prefer but ending with .git, for example:
- Enter the new directory created above
- Input this command to initiate a “bare” Git repository:
git init --bare
Preparing a Git Post-receive Hook
Git uses special “hook” files to perform certain actions at different stages. In our example, we are creating a hook that will initiate every time we do a
git push to our remote repository. This hook will realize we have made a push and will automatically check out our project files to a directory of our choosing. By choosing a live directory on your server, you will effectively be making your files available to the public. This is our goal. It is also for this reason that we must reiterate: make sure your project does not contain any sensitive information.
- Enter the new repository from above
- Use a
lscommand to check if you have a “hooks” directory; the output should show one
- Enter the
- Create and edit a file called “post-receive”; assuming your favorite text editor is Nano, the command would look like this:
- Copy and paste the following into the post-receive file, substituting “userna5” with your cPanel username:
#!/bin/sh git --work-tree=/home/userna5/public_html --git-dir=/home/userna5/production.git checkout -f
- Exit the text editor
- Make the post-receive file executable with the following command:
chmod +x post-receive
Now our remote production repository is ready to receive our push, we just need to go back to our local working directory to add the InMotion Hosting server to our list of remote repositories.
Adding your Remote Repository on the Local Side
The final steps of this tutorial will take on place on your local computer, where you have all the project files.
- Open a shell on your local computer and use
cdto navigate to your project directory
- Input the following command, substituting “name-of-repo” with a title of your choosing (i.e. “live”, “production”, “web”, “IMH-server”, etc.), userna5 for your cPanel username, and “destination” for the primary domain or IP address of your server:
git remote add name-of-repo userna5@desination:/home/userna5/production.git
- Commit or add files as needed and use the following command to deploy your files to the remote repository:
git push name-of-repo master
- When prompted for a password, use your cPanel password
You have completed this tutorial! Well done. You now know how you can use Git to publish your project files to your server. Be sure to check out our guide on how to deploy files with GitHub Actions for FTP and SCP.