Skip to content Skip to footer navigation

Setting up Statamic v3 on MacOS Part 4 of 4 (Aside) Working with Git

Update – 29th November 2022

I wrote these posts when I was pretty new to Statamic, so proceed with caution. I'll leave them here for preservation and amusement for my future self, but I'd probably do things slightly differently now. These days there are a couple of really great structured ways to learn Statamic: [Jack's official course on Laracasts](https://laracasts.com/series/learn-statamic-with-jack), not to mention [Jonas' excellent course](https://statamictutorials.com/).

In this post, I'll cover how to get your project into Git and get your machine to talk with a Git repository-hosting service such as Bitbucket. Using Git means we can save our project in different states, roll changes back if we make a mistake and also it allows us to push up/pull down changes to the website and its content.

Authorising Your Computer with SSH in Bitbucket

FYI if you've decided to store your files in a Github repository instead, this process will be very similar.

  1. Go ahead and set up a Bitbucket account if you don't already have one.

  2. When you log in to Bitbucket, you should be in the “Repositories” dashboard. From here look in the left-hand corner where you should see your avatar. Click this and then “Personal Settings” then “SSH Keys”.

  3. We now need to generate an SSH key on our machine. Open the Terminal application and paste in ssh-keygen -t ed25519 -C "your_email@example.com", substituting the email address for whatever your Bitbucket email address is.

  4. Once a key pair has been generated, you'll be prompted to enter a file to save the key. Press enter to accept the default location.

  5. You'll now be prompted to generate a passphrase. This is basically a password. Please make a note of whatever you type in because you'll need it later. Terminal should now inform you that a fingerprint has been generated.

  6. Back in Bitbucket press the add key button

  7. Give the key a label; for example, the name of the machine you've used to generate the key. In my case, I called it MacBook 2018

  8. At the time of writing Bitbucket prompts you to copy the key from your machine using a command that references an older encryption method (RSA). Since we've generated a key using a newer encryption standard, an RSA copy command won't work. To be sure you get the key, you can copy it directly from the file. Go to your home directory and make sure hidden files are showing in Finder—you can toggle these on/off using the keyboard shortcut cmd + shift + .

  9. You should see a .ssh directory. Inside the folder look for id_ed25519.pub. Open this in a text editor and copy the contents of this file. Paste this into the key field in Bitbucket, then press Add key.

Create a New Repository in BitBucket

In the repository dashboard in Bitbucket create a new repository. You can call the Repository whatever you like, but usually, you'll call it the name of your site. I find it useful to specify the site's technology, for example for my own site I've called it “jaygeorge-statamic”. This means if I ever switch to a different CMS in future (which I have done in the past!) I can store that as a separate repository, e.g. “jaygeorge-perch”.

I find it easier to select “no” when it asks me if I'd like to create other files—I prefer to create the repo locally and manually add these things. This is just personal preference though—you may find it easier to let Bitbucket do this for you.

Creating a Project in Git

It's now possible to clone down the repository using the Sourcetree app. This may be the simplest way to do things, but here I'll cover creating the repository locally from scratch and linking it to Bitbucket.

  1. If you don't already have a project in your Sites folder go ahead and make one, e.g. create a subfolder in Sites called test

  2. We now need to make sure we're in that project folder in Terminal. In Finder press cmd + c on the project folder to “copy” it. Back in Terminal type cd (with a space afterwards) and then press cmd + v to paste the directory path we've just copied. Hit enter

  3. The Terminal window's top bar should now show the name of the project folder we're in.

  4. Now we're going to create a Git project. To do this run git init and hit enter. Now run git add . – this command will add all your files to the repository. Now run git commit -m "Initial commit" – this command “commits” the files you've just added. The -m stands for “message”.

Adding Bitbucket as a Remote Repository

We've got our repository set up locally, and now we want to synchronise it with a host (in our case Bitbucket). Eventually, we will set up our production server to “pull” our code from Bitbucket—but that's for another day!

  1. Once you've created a repository back on the Bitbucket site, it should give you a few different sample commands. We can skip a few of these since we've already created a git repository, added files, and committed. We need to add the Bitbucket repository we've created as a “remote” now. You should see the command we need to run on the Bitbucket site, in my example it was git remote add origin git@bitbucket.org:jaygeorge/test.git.

  2. push up your commits to the master branch with git push -u origin master. If Terminal tells you the authenticity of the host can't be established say yes to continue. At this point, you'll be prompted to enter your passphrase that we made up earlier. Do so and hit enter (it'll be invisible so don't worry if you don't see anything while you're typing)

Once you've “pushed up” the code, at this point if you go back to Bitbucket and refresh the page you should see your commit along with its message. PS If you try to push to the repository again and get a permission denied public key message, try restarting your computer and trying again.