Post

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 several times before it works. Its best to set up an external text file so you don’t find yourself retyping the same info.

Prerequisites

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

1
2
3
4
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.

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

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

1
chmod +x post-receive

Locally, Add a Remote:

  • Navigate to your local repository using the terminal,
1
2
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

1
2
3
4
5
6
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 remote url for the repository on the remote server.

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

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

1
git push

Troubleshooting

1
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) in regards 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.