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.
Go ahead and set up a Bitbucket account if you don't already have one.
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”.
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.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.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.
Back in Bitbucket press the
add key
buttonGive 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
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 + .
You should see a
.ssh
directory. Inside the folder look forid_ed25519.pub
. Open this in a text editor and copy the contents of this file. Paste this into the key field in Bitbucket, then pressAdd 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.
If you don't already have a project in your
Sites
folder go ahead and make one, e.g. create a subfolder inSites
calledtest
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 typecd
(with a space afterwards) and then presscmd + v
to paste the directory path we've just copied. Hitenter
The Terminal window's top bar should now show the name of the project folder we're in.
Now we're going to create a Git project. To do this run
git init
and hit enter. Now rungit add .
– this command will add all your files to the repository. Now rungit 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!
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
.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 sayyes
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.