Using Git to Publish Files

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.

  1. Log into your VPS/Dedicated server via the shell
  2. create directoryCreate a directory in your Home directory named whatever you prefer but ending with .git, for example:
    production.git
     
  3. change directoryEnter the new directory created above
     
  4. initiate github repoInput 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.

  1. enter repo from command lineEnter the new repository from above
     
  2. use ls commandUse a ls command to check if you have a “hooks” directory; the output should show one
     
  3. hooks directoryEnter the hooks directory
     
  4. create fileCreate and edit a file called “post-receive”; assuming your favorite text editor is Nano, the command would look like this:nano post-receive
     
  5. 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
  6. Exit the text editor
  7. 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.

  1. Open a shell on your local computer and use cd to navigate to your project directory
  2. 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
  3. 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
  4. 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.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

5 thoughts on “Using Git to Publish Files

  1. 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

    1. 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.

  2. 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.

  3. 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.

Was this article helpful? Join the conversation!