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:
production.git
- 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
ls
command to check if you have a “hooks” directory; the output should show one - Enter the
hooks
directory - Create and edit a file called “post-receive”; assuming your favorite text editor is Nano, the command would look like this:
nano post-receive
- 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
cd
to 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.
hey, just one thing you forgot, this was missing and stopping me from pushing files… github changed their auth requirements so login/pw doesnt work anymore
git remote set-url origin [email protected]:username/your-repository.git
Thanks for the comment, Penelope! In the article we’re mainly focusing on setting Git up on a private server. Instructions for GitHub will differ a bit from what we have here. You may want to try setting up SSH keys with GitHub, or one of the alternate authentication methods they support.
It’s never easy. After I reach the end of the guide I get: fatal: ‘…/home/laravelfoodorders.git’ does not appear to be a git repository.
A few things:
Thank you for this excellent article! It is so much easier to update my site now that I have this set up. The only thing that tripped me up was on step 5… #!/bin/sh should be on its own line, so there should be two lines in the post-receive file.