Deploy files to a remote server using git push

To deploy files to a remote server using Git, you can follow these general steps. As you may need to do this process serveral times before it works. its best to set up an external text file so you dont find yourself retyping the same info.


Assuming you already have added your ssh key to the authenticated keys on the remote server.

Create an empty Git Repository on the Remote Server:

Here we connect via ssh to the remote server and manually create the repo

ssh user@remote-server
mkdir /path/to/your/repo.git
cd /path/to/your/repo.git
git init --bare

Set up a post-receive hook

To automatically deploy your files on the remote server after a push, you can use a post-receive hook. Create a file named post-receive in the hooks directory of your bare repository on the remote server.

cd /path/to/your/repo/hooks

Create the post-receive file

You could also use nano if you dont like vim. the following command will create a new empty file in your current directory.

vim post-receive

Add Deployment Script to the Post Receive file

You need to add the following script to the post-receive file. Making some adjustments to the script to accomidate your file directory structure. I find my git repo is stored in the same directory i am deploying too. So my to file paths are very similar.

  1. The first file path points to the directory I want to deploy to.
  2. The second file path points to the git repo directory we created previously.
git --work-tree=/var/www/your-directory --git-dir=/var/www/your-directory/repo.git checkout -f

Once that script is added to your post-receive file and you have adjusted the file paths to suit your file structure. Save the file and close it down.

Make Executable

Now lets update the file permissions so that it is executable.

chmod +x post-receive

Locally, Add a Remote:

  • Navigate to your local repository using the terminal,
git add .
git commit -m "Your commit message"

Use your SSH Config file for SSH authentication

Now to push from local to the remote repo, you need ssh key authorization. If your ssh key has been added to the server great! Now you just need to configure a sshconfig file to help streamline the deployment to the server.

Creating an SSH Config file

Add the following as a new SSH Config file entry

host testrepo
User serveruser
HostName Host_IP_Address
IdentityFile ~/.ssh/id_rsa
ServerAliveInterval 120
ServerAliveCountMax 30

Add the git remote url

Using the sshconfig file entry for authorization, add the remote url for the repository on the remote server.

git remote add origin testrepo:/path/to/your/repo.git

Push to the remote

Once the remote is set up, you can push your commits to it. The following command pushes your local branch to the remote repository. Replace main with the name of your branch.

git push -u origin main

Remember to replace main with the name of your branch if it’s not the main branch. Adjust paths and server details according to your specific setup. The -u option sets up tracking, so in the future,

Now your local changes are pushed to the remote server. Subsequent pushes can be done simply with:

git push


git push -v site_repo master

Access Denied

access denied

Remote: fatal: You are on a branch yet to be born

This error is pretty subtle. Its hidden in the log.

I have a not so graceful fix for this.

  • Delete your local repo
  • Create new local repo
  • Add your remote repo url
  • push to repo

Get the Error

In order to fix the error you need to know what the error is. Ensure your git push is using the verbose output so you are getting all the information (errors included) inregards to the push command.

Changes not reflected on the site

Sometimes there is no error and the deployment seems to work. But the files are not deployed to the server. If your pushing but cant see the changes reflected on the site.

  • Check your post receive file paths are correct
  • Check the git push log for any errors (sometimes they are not obvious)

Fatal: cannot exec ‘hooks/post-receive’: Not a directory

This means something is wrong with your post-receive file.

  • it might not be executable (permissions error)
  • it might have some comments at the top of the file (they cause issues, remove the comments)

More Info

more info

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.