Add project repository to Gitlab

If you want to install your project elsewhere (like on your local computer) and exchange edits with the live version, then you'll want to host your Git repository online somewhere. All project locations can then push and pull from that central repository, so each installation will always have access to the latest data.

You probably don't want this data to be accessible by anyone else, however. So you need the central repository to be private. There are several providers for private Git repositories, such as Github and Bitbucket, but we decided to use Gitlab for this purpose. Gitlab offers unlimited private repositories for free, together with a bunch of other features that you might find interesting.

The project repository on Gitlab will be your single source of truth. Whatever happens in the different locations: always make sure that Gitlab contains the latest, working version of your project.

Create an account on Gitlab

Obviously, you need to have an account. Go to https://gitlab.com/ and sign up.

FYI: you can also host Gitlab yourself, because it's open source software.

Create a private repository

This means: create a new project and make sure access permissions are set to private. Copy the 'Clone via SSH' link when you're done.

Add SSH key to Gitlab

Now go back to your server. Generate an SSH key as your projects' local user:

sudo chown project-user:project-user /home/project-folder/.ssh
sudo -i -u project-user ssh-keygen -t ecdsa -b 521

Press enter when asked in which file to save and twice when asked for a passphrase. Then:

cat /home/project-folder/.ssh/id_ecdsa.pub

Copy the output of this command (which is your new public key) and add it to Gitlab as Deploy Key. In your project on Gitlab, go to Settings > Repository > Deploy keys. If you are installing the project on your localhost also, make sure 'Write permissions' is checked, so you can merge changes from your localhost with the live project.

You can also add the public SSH key of your local machine to your Gitlab user account. Then, you'll automatically have read/write access to all the repositories in your account. Useful for local development if you run Nginx or Apache under the same user account for all projects, but I wouldn't recommend this on a production server with multiple projects. One compromised site would open the door to all the others, so better to create a new Linux user for each project and isolate php-fpm to run under that user as well.

Add Gitlab repository as remote

We're ready now to deploy our local repository to Gitlab! Go back to the project root on your server and add the Gitlab location as remote:

git remote add origin [email protected]:fractal-farming/your-project.git

And the final push:

git push -u origin master