Skip to content

Instantly share code, notes, and snippets.

@WesThorburn
Last active March 21, 2023 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WesThorburn/9ac3fb5713b85e31714d9e9052e8d19c to your computer and use it in GitHub Desktop.
Save WesThorburn/9ac3fb5713b85e31714d9e9052e8d19c to your computer and use it in GitHub Desktop.
Deploying Git Project Repositories to an Ubuntu server

Deploying Git Project Repositories to an Ubuntu server

  • If you haven't already, install git on the server by running: sudo apt-get install git
  • If you haven't already, create a repo directory inside of /var by running sudo mkdir repo
  • On the live server, go into /var/repo and create a new directory inside eg newsite.git
  • The new directory path should look like /var/repo/newsite.git
  • Ensure a new directory has been created in the www directory. Eg /var/www/newsite
  • cd into /var/repo/newsite.git and run: sudo git init --bare --bare means that our folder will have no source files, just the version control.

A number of new folders will be created, cd into the hooks directory

  • Create a new 'post-receive' file With the following contents:
#!/bin/sh
git --work-tree=/var/www/newsite --git-dir=/var/repo/newsite.git checkout -f
  • Set the post-receive file permissions by running: sudo chmod +x post-receive Set the var/repo/newsite.git directory permissions by running: sudo chown -R username:group newsite.git Set the var/www/newsite directory permissions by running: sudo chown -R username:group newsite Go into the repository in the local directory and run: git init if you haven't already Add the remote repository by running: git remote add remote-name ssh://user@mydomain.com/var/repo/site.git

Push the files to the server by running the normal three commands. git add . git commit -m "Initial Commit" git push remote-name master

This following section is only required if your server only allows private key access. This guide assumes that you've added the public key to the server for the user you've specified in the 'ssh://user@domain.com/var/repo/newsite.git' address.

This StackOverflow post is where this process came from: http://stackoverflow.com/questions/2127104/permission-denied-publickey-error-using-git-on-windows-7

If you try to run git push live master on a Windows machine and get an error such as "Permission Denied (Public Key)," follow this process to fix it.

The "Permission Denied (Public Key)" error is most likely caused by git trying to provide the default id_rsa file as the private key file. If the server isn't set up to accept the key provided in the id_rsa file, it'll reject the connection straight away. By default, only the id_rsa key will be provided and multiple keys cannot be provided unless you use plink and pageant.

First, download and save the plink and pageant executables, preferably saving them in a directory that's easy to access. Open pageant, and add the private key for the server you're trying to push to. In the local command line application, set the GIT_SSH environment variable by running: set GIT_SSH=C:\Users\username.ssh\plink.exe (Adjusting this address to match the location of your plink.exe file) Double check to make sure pageant is running. Then run git push live master again, the server should now accept the key file.

For more info: https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

@DYW972
Copy link

DYW972 commented Jun 19, 2021

Hey 🖖,

Thank you for this gist!

To use git --bare and apply this git auto-deployment system, How do you manage permission for git when /var/www is ownership is root:root?

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment