Setting up a project is both stressful and fun! One of the first decisions you’ll make is where to store your code. Somewhere that’s easy to use and easy to integrate with other tools normally tops my list. I use Bitbucket and Sourcetree for many of my projects and urge you to give them a go. This post will cover installing Sourcetree, connecting Sourcetree to a Bitbucket account, and creating a Git repository with Sourcetree using a Mac. This guide assumes you’ve already set up a Bitbucket repository. If you haven’t already done this, first check out Atlassian’s comprehensive guide to creating a Git repository.
A Definition of Git
- [A] random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
- Contemptible and despicable. Simple. Take your pick from the dictionary of slang.
- “Global information tracker”: you’re in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
These descriptions aren’t meant to be taken seriously and are just a bit of fun. However, there’s some truth to those descriptions—Git can be a little frustrating at times. However, the official description from git-scm.com may give you a better idea of what Git is used for: “Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.”
An Explanation of Sourcetree and Bitbucket
- Displaying a visual representation of your Git Repository, both local and remote.
- Organizing the visibility of commands so the most common Git commands are the easiest to select.
- Providing guidance on which commands are possible to perform at any stage of the Git workflow by enabling and disabling buttons.
Bitbucket is a web-based version control repository hosting service, owned by Atlassian, for source code and development projects that use either Mercurial or Git revision control systems. Sourcetree and Bitbucket integrate closely, are easy to connect, and are a great option when you’re starting to use Git.
Install Sourcetree on Your Mac
The following instructions detail how to download and install the application on your Mac:
- Download the latest version of Sourcetree from the official downloads page.
- Unzip the downloaded zip file by double-clicking it in a Finder window.
- Copy the extracted .app file to the Applications folder.
- Open the Applications folder in Finder and double-click on the Sourcetree icon.
If the steps are all successful, your Sourcetree application should look like the image below:
This image shows the initial view when opening the Sourcetree application. The application defaults to the local tab. On a new install, you’ll have no local repositories and the list will be empty.
Connect to Your Bitbucket Account
Connecting to your Bitbucket account from Sourcetree will synchronize your source code between your computer and Bitbucket. To connect Sourcetree to Bitbucket, follow these required steps:
- Check for existing remote repositories.
- Configure the connection.
- Authorize Sourcetree with Bitbucket.
- Set up SSH keys.
Check for Existing Remote Repositories
Select the Remote tab in the Sourcetree application to check for any existing repositories connected to accounts.
The above view shows the Remote tab view without any repositories. A new installation of Sourcetree won’t be connected to any accounts. To connect your Bitbucket account, click the Connect… button. This will open the Accounts view of the application.
Configure the Connection
Click the Add… button to open the view shown below.
The above view shows that no account is currently connected and no SSH key is configured. Choose the following for the given drop-down boxes:
- Host: We’re connecting to a Bitbucket host, so leave this value set to the default.
- Auth Type: OAuth provides the best security for multiple connections to Bitbucket, so leave this value set to the default.
- Protocol: SSH is the most convenient connection because it allows your computer to trust the messages sent to and from Bitbucket. So leave this value set to SSH.
Authorize Sourcetree to Connect to Your Bitbucket Account
To authorize Sourcetree to connect to your Bitbucket account and provide Sourcetree with an OAuth key for future communications, click the Connect Account button. This will open a browser window requesting login details for the Bitbucket website, as shown below.
The above image shows the login webpage for Bitbucket. Log in to Bitbucket and open the webpage with Sourcetree. The page will redirect back to the Sourcetree application and provide the application with an OAuth token automatically. This allows the Bitbucket to trust any future messages coming from the Sourcetree application.
The view above shows a successfully connected Bitbucket account. The options are as follows:
- Bitbucket username: The unique username that you gave on signup. I’ve chosen “peterjgrainger.”
- Bitbucket profile picture: This is an optional profile picture you set up when creating the account.
- Communication protocol: SSHis the most convenient and safe way to connect Sourcetree to Bitbucket.
There’s a warning sign (⚠️) beside the text “SSH.” Although we’ve connected Sourcetree to our Bitbucket account, our computer’s SSH key isn’t stored in Bitbucket.
Setting up SSH Keys for Bitbucket
To enable cloning of a repository locally using Git, create an SSH key for that account. Double-click on the account you’ve just made and click the Generate Key button.
The above image shows the “Create an SSH Key” view. To add extra protection to your private SSH key, encrypt it with a passphrase. Press the Create button to make a private and public SSH key and automatically upload the public key to Bitbucket. Press Save and the application will return to the Accounts view.
The Accounts view now no longer shows the warning sign ⚠️ beside SSH, and Sourcetree is fully set up.
Using Sourcetree to Manage Your Bitbucket Repository
Create a Private Git Repository on Bitbucket
A repository is a store for a group of related files. Most people store the source code needed to run an application, but you could store code for multiple applications or a collection of plain text files. How you group your files is up to you. Creating a repository as a private repository allows only you to access and modify the contents.
To create a new private Git repository on Bitbucket, click the New… drop-down.
The image below shows the options available when creating a new repository. Select Create Remote Repository from the list to open the “Create a remote repository” view.
The following choices will create a private Git repository named ASPE-workshop:
- Account: This defaults to your current default account (here, Bitbucket – peterjgrainger).
- Owner: This defaults to the user on the default account (peterjgrainger).
- Name: Choose a name that describes the files in the repository. This will be converted to lowercase (ASPE-workshop).
- Description: This is an optional field that describes what the files are. In the example above, I’ve left this blank.
- Type: Choose Git as the type of repository.
- This is a private repository: Check this value to create a private repository visible only to you and those you invite.
Click the green Create button to make the remote repository.
You’ll see an extra repository in your remote repositories list showing your username, a forward slash (/), and the name you gave your repository. The image below shows the repository I made using the instructions in this blog.
Use Sourcetree to Manage Your Repositories
Sourcetree is a great way to start using Git repositories without the steep learning curve of using the command line. Bitbucket integration also makes it easy to connect to an account and enable the creation and administration of repositories directly from the application.
Once you’re comfortable with the basics, moving to the command line will be much simpler. You’ll already understand the concepts of Git, so the power of the command line will be a lot easier to handle.