The practical benefits of a remote source code repository

Have you ever accidentally gone too far deleting stuff on your computer? No? Well, you’re the freak!

But seriously, it’s a really easy thing to do, especially when you’re on autopilot. One minute you’re casually “rm -rf”ing all over the place and the next you’ve blown away your project. If you’re using a version control system like git you can recover from many serious setbacks. If you also deleted your .git folder, however, you’re SOL. Not so if you’ve been pushing your code to a remote repository!

Using remote repositories, you can easily export your work in progress to any number of machines. This is especially handy if you intend to allow others to collaborate with you on development.

Why bitbucket?

So why bitbucket, as opposed to github? The primary reason is that private repositories are entirely free on bitbucket, so long as no more than 5 users belong to a given team. At the time of the original writing in 2017, Github costed $7/month for private repos. Gitlab is another equally viable option here, as it also offers free private repos…

Among other things, keeping your code in a public repository opens the possibility of exposing sensitive data, such as secret keys and authentication tokens, if you are not careful. Every so often articles will circulate around the internet about all the sensitive data that can found on github via basic searches. This is a big reason why it is often considered a best practice to store sensitive information in environment variables as opposed to variables in your script.

Set up a local git repository

Let’s start by creating a local git repository which we can later push to a remote repository.

If you followed the web scraping series, you should already have a local git repo you can use for this. If not, let’s create a new folder, initialize a git repo, and commit an empty file.

$ mkdir dummy_repo
$ cd dummy_repo
$ git init
$ touch foobar
$ git add foobar
$ git commit -m "add foobar"

Sign up for bitbucket

Head over to bitbucket.org and sign up for an account using a valid email address.

Copy your public key to bitbucket

Click on your bitbucket avatar and navigate to “Bitbucket settings”, and then “SSH keys”, and click on the “Add key” button.

If you already have ssh configured on your machine, you can copy the output from “cat ~/.ssh/id_rsa.pub” into the “Key” form.

If you have not created ssh keys for your computer, bitbucket’s guide is actually pretty solid and will guide you through the process.

Create a repo on bitbucket

Click on the “Repositories” tab on bitbucket, and again on “Create repository”.

Add the bitbucket repo as a remote repository

Within your local git repository, add the bitbucket repo as a remote repository, substituting your own username and repo.

$ git remote add origin [email protected]/andythemoron/dummy_repo.git

Push commits to the remote repository

You can now push your local commits to the remote bitbucket repository. The below command will push your local commits to your remote bitbucket repository, and set the bitbucket repo as your “upstream” reference.

$ git push -u origin master

This means that now you can simply push or pull without having to explicitly specify the source and target every time.

$ touch foobar2
$ git add foobar2
$ git commit -m 'add foobar2'
$ git push

Woila!