Getting started with private projects on Raspbian and Bitbucket

Getting started with private projects on Raspbian and Bitbucket
Published: by

Bitbucket is a great alternative to Github to start, store and track simple programming dependant project files. On a Raspberry Pi it can be done using Raspbian. In addition to Github, projects uploaded to Bitbucket with less than 6 contributors can be kept private and won’t cost a cent.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

Many of my projects at home use a Raspberry Pi with Raspbian installed. The files that are needed in these projects (e.g. Python, HTML and Bash script files) often run the risk of being lost, e.g. after the crash of a SD card, or might need to be reproduced on a different Raspberry Pi. By using Bitbucket these files can be safely stored (‘pushed’) on, and retrieved (‘pulled’) from, their remote server using Git, Mercurial and terminal commands. This post will show how to create new project files from Bitbucket to be used on a Raspberry Pi and how to upload existing project files from a Raspberry Pi to Bitbucket.

Any web browser can be used to access Bitbucket and create an account. For the linking Raspberry PiRaspbian was used as the operating system and a connection to the internet will be required. In the case where a screen and keyboard is not connected, remote access to the Raspberry Pi can be obtained using PuTTY.

Creating a Bitbucket account for first time users

Creating an online Bitbucket account is straight forward. With access to the GUI, a Bitbucket account can be created using Raspbian’s Chromium or the web browser of any other device, e.g. a PC. An Atlassian account will be automatically created using the same email, username and password. The account has to be activated by using a URL that will be sent to the account email address.

The account password is the same password that will be used to ‘push’ and ‘pull’ repositories (see later). It is therefore recommended to choose a strong password, but one that is still easy to type off-hand.  The password can always be changed later by requesting a password reset. The account username will be used as part of your repository link(s).

Before we drop you in Bitbucket, help us tailor your experience

By stating during the account setup that you have no experience with Bitbucket (i.e. for first-time users), the initial layout will include additional little tutorials and tips (recommended).

Creating your first Bitbucket repository

After creating a Bitbucket account, first-time users will be guided through the process of creating a new repository or importing an existing one (see below). A repository is basically a central place where files are stored. It can also be seen as the directory where the project files are being stored or the name of the project.

Creating a first repository with Bitbucket

While creating a new repository, the name of the repository, whether it should be private or public, to include a README (choose yes), the version control system (Git or Mercurial – see below), the description and language are the more important options that has to be supplied.

Private repositories will be protected using the account password. In order for other people to also use the repository on their own Raspberry Pis, the repository can be set to public or they will need your password to do so.Bitbucket - Create a new repository

Git vs. Mercurial

Both Git and Mercurial are established revision-control tools. Assuming that the project files are to be situated locally on the Raspberry Pi‘s SD card, Git or Mercurial can be used to ‘clone’ current project files from a  Bitbucket repository, or  ‘push’ and ‘pull the latest updates of the files to and from Bitbucket.

One nice thing about revision-control tools is that they will keep track of the most recent versions of these files. While both Git and Mercurial can be used using terminal commands, Mercurial can also be used from a Linux or Windows GUI. For each repository, one or the other needs to be chosen.

Bitbucket myrepository
Now your first Bitbucket repository has been created.

When looking at the repository screen (in this case the newly created, “myrepository”) there are a few important things to take note of:

  • The name of the repository is also the directory the files will be created in
  • The location of the repository (that will be used to clone or connect to the repository)
  • The names of the files in the repository (in this case README. md)
  • The date of the last commit
  • The message that was supplied with the last commit

Importing existing repositories into Bitbucket

Existing repositories can be easily imported into Bitbucket from Codeplex, Git, Mercurial and SourceForge. After supplying the existing URL, options similar to those for creating a new Bitbucket repository (see above) will be given. Private repositories will require a user name and password.

Using Git terminal commands on the Raspberry Pi

For those who still prefer to (only) use terminal commands to create and maintain project files Git is a good first choice. Terminal commands for Git and Mercurial (see later) are very similar, but only one or the other (set while creating the repository) can be used. To install Git on Raspbian, the following terminal command can be used:

sudo apt-get install git

Before being able to use a repository locally, it first needs to be cloned. Apart from creating the first copy of the repository, the clone command will also set up additional files needed for Git to recognise the repository.

When a repository is cloned, it will be moved into the repository name within the current working directory. While in the desired parent directory, the Git command to clone a repository is as follows:

git clone https://yourusername@bitbucket.org/yourusername/myrepository.git

The account password will be required with private repositories. After the repository has been cloned onto the Raspberry Pi, current files can be changed and/or new files can be added.

Updated and new files can be ‘pushed’ back to Bitbucket. Before being able to do this, you will need to tell Bitbucket who you are (only required once per repository). The following command lines need to be entered while inside the repository directory:

 cd myrepository

will move into the repository directory.

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

where “you@example.com” and “Your Name” will be the same as your account email and the full name supplied while setting you the Bitbucket account.

git add --all

is used to tell Git to that new or changed files need to be uploaded.

git commit -m '<commit_message>'

is used to commit new files/changes to the local repository where the <commit_message> is anything that describes the changes you are committing.

Then finally to upload the actual files, the Git ‘push’ command is used:

git push

The account password will be required with private repositories.

In the case where the repository files were updated and the older files are still on a local Raspberry Pi, i.e. where project files have to be updated on a different Raspberry Pi, you can simply use the Git ‘pull’ command. The pull command can only be used after the initial clone command (see earlier). While inside the repository directory, the Git pull command can be used as follows:

git pull

Using Mercurial terminal commands on the Raspberry Pi

The terminal commands for Mercurial and Git (see earlier) are very similar, but only one or the other (set while creating the repository) can be used. To install Mercurial on Raspbian, the following terminal command can be used:

sudo apt-get install mercurial

When a repository is cloned, it will be moved into the repository name within the current working directory. While in the desired parent directory, the Mercurial command to clone a repository is as follows:

hg clone https://yourusername@bitbucket.org/yourusername/myrepository

The account password will be required with private repositories. After the repository has been cloned onto the Raspberry Pi, current files can be changed and/or new files can be added.

Updated and new files can be ‘pushed’ back to Bitbucket. Before being able to do this, you will need to tell Bitbucket who you are (only required once per repository). The following command lines need to be entered while inside the repository directory:

 cd myrepository

will move into the repository directory.

hg add

is used to tell Mercurial that new or changed files need to be uploaded.

hg commit -m '<commit_message>'

is used to commit new files/changes to the local repository where the <commit_message> is anything that describes the changes you are committing.

Then finally to upload the actual files, the Mercurial push command is used:

hg push

The account password will be required with private repositories.

In the case where the repository files were updated and the older files are still on a local Raspberry Pi, i.e. where project files have to be updated on a different Raspberry Pi, you can simply use the Mercurial ‘pull’ command. The pull command can only be used after the initial clone command (see earlier). While inside the repository directory, the Mercurial pull command can be used as follows:

hg pull

Uploading existing project files from the Raspberry Pi to Bitbucket

While new repositories can now be created from Bitbucket, there might be current project files that is still only on the Raspberry Pi. Although the process is slightly different, these files can be pushed onto a Bitbucket repository for version control and central preservation.

To keep things simple, the current directory the project files are situated in should also be used as the repository name. In this case “mycurrentproject” will be used and the files is situated in the /home/pi/mycurrentproject directory.

Bitbucket - Create a new repository

To start off, a new repository can be created within Bitbucket by clicking on the + icon situated on the left of the overview (Your work) screen. If your project already contains a README.md file, don’t create a new one. A readme file can always be created at a later stage.

Bitbucket - mycurrentproject

From the Raspberry Pi, navigate to the project directory. In this case it will be /home/pi/mycurrentproject:

cd /home/pi/mycurrentproject

To connect your project to its Bitbucket repository, the following set of commands should be used (Git was selected for version control):

git init
git remote add origin https://yourusername@bitbucket.org/yourusername/mycurrentproject.git

Tell Bitbucket who you are (only required once per repository):

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

where “you@example.com” and “Your Name” will be the same as your account email and the full name supplied while setting you the Bitbucket account.

git add --all

is used to tell Git to that new files need to be uploaded.

git commit -m "<commit_message>"

adds the commit message. Then finally do the Git push:

git push -u origin master

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a shit. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More Raspberry Pi related posts

Discover the easy way to send bulk SMS from your PC, Mac or mobile device
Discover the easy way to send bulk SMS from your PC, Mac or mobile device
19 November 2018
Using the latest in technology, WinSMS offers cost effective messaging solutions that makers and business owners can use. With their online portal and API functionality, buying SMS bundles and sending them to recipients is more cost effective and easy to do. More…
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.