Bitbucket is a great alternative to Github to start, store and track coding projects - privately. Raspbian users can also use Bitbucket to manage projects.
Table of contents
Introduction to private projects on Raspbian and Bitbucket
Bitbucket is a Git-based source code repository hosting service owned by Atlassian. It is a great alternative to Github to start. On a Raspberry Pi, Bitbucket can be used using Raspbian. Projects uploaded to Bitbucket with fewer than 6 contributors can be kept private and won’t cost a cent.
For Raspberry Pi users, project files (e.g. Python, HTML, CSS, JavaScript, Bash script, etc.) often run the risk of being lost, e.g. after the crash of an SD card, or might need to be reproduced on a different Raspberry Pi. By using Bitbucket these files can be tracked, stored and retrieved from a remote location using Git and terminal commands.
This post will show how to upload existing project files from a Raspberry Pi to a Bitbucket repository and how to download/copy new project files from Bitbucket to be used on a Raspberry Pi.
Any web browser can be used to access Bitbucket and create an account. For linking Raspberry Pi, Raspbian (now known as the Raspberry Pi OS) was used as the operating system and a connection to the internet will be required.
In the case where a screen and keyboard are not connected, remote access to the Raspberry Pi can be obtained using PuTTY.
Raspbian is a Linux-based operating system, but Bitbucket and the same Git commands can be used in many other places such as IDEs and Windows (using Git Bash). To be able to use Git commands on Windows, Git needs to be downloaded from Git for Windows or from the Git official website (all operating systems) and installed.
- Get the Corsair K95 RGB Platinum XT Mechanical Gaming Keyboard from Amazon.com
- Get the LG 22MK430H-B 21.5″ Full HD Monitor from Amazon.com
- Get the ROCCAT Burst Pro PC Gaming Mouse from Amazon.com
Creating a Bitbucket account for first-time users
Creating an online Bitbucket account is straightforward. With access to the GUI, a Bitbucket account can be created using Chromium or the web browser of any other device. An Atlassian account will be created automatically 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).
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 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. A repository is basically a central place where files are stored. It can also be seen as the main local directory/folder where the project files are stored or as the name of the project.
While creating a new repository, the name of the repository, whether it should be private or public, to include a README file and the description are the more important options that have to be supplied. By default, Bitbucket will use Git as the version control system.
Private repositories will be protected behind your login and the password of the account. In order for other people to use a private repository, the repository needs to be set to public or they will need your password to do so.
Multiple repositories can be created with Bitbucket.
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 connect to the repository)
- The names of the files in the repository (in this case there is only a README.md file)
- The date of the last commit
- The message that was supplied with the last commit
Now a new, basically empty repository is created which is ready for some files. Later on, this post will show how to add files from the Raspberry Pi to this repository and then how to copy/clone the files back onto a different Raspberry Pi.
Git and Git commands
To be able to track file changes and to create a connection between the Raspberry Pi and Bitbucket, Git commands will be used. Git is an established revision-control tool. It keeps track of all the changes that occur with each file, allows branches and allows collaboration between different authors. It also allows connections between your projects and an online repository (e.g. created with Bitbucket).
Another popularly used revision-control tool is Mercurial. Before Git can be used on an operating system, it needs to be installed first.
To install Git on Raspbian, the following terminal command can be used:
sudo apt-get install git
Git only needs to be installed once.
Git commands are given using a terminal (also referred to as the command line). On the GUI version of Raspbian, the terminal icon is situated on the top left of the screen.
Assuming that the project files are 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.
Uploading existing project files from the Raspberry Pi to a Bitbucket repository
Earlier in this post, it was shown how to create a new Bitbucket account and how to create a new repository. This section will show the process and Git commands to upload (‘push’) new files from a Raspberry Pi to a Bitbucket repository for tracking and central preservation.
The next sections will show how to clone a Bitbucket repository to a Raspberry Pi, how to update a Bitbucket repository from a Raspberry Pi and how to get the latest updates from Bitbucket.
To keep things simple, the current (working) directory the project files are situated in can also be used as the repository name. In this case, “mycurrentproject” will be used and the files are situated in the /home/pi/mycurrentproject
directory.
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. For now, don’t create a readme file. A readme file can always be created at a later stage.
Now a new, basically empty repository is created which is ready for some files. From the Raspberry Pi, navigate to the project (working) directory. In this case, it will be /home/pi/mycurrentproject
:
cd /home/pi/mycurrentproject
To list the files in the current directory, thedir
or ls
command can be used:
ls
To clear the terminal from all previous data, the clear
or cls
Bash commands can be used:
clear
To connect your project to a Bitbucket repository, Git needs to be initialised first. This is done using the git init
command:
git init
The Git init command will basically set up Git in the current local folder. This means that you can now start using Git.
If this is the first time that you initialised Git in the current local (working) directory, you need to tell Git who you are by using the following two Git config commands (only required once per repository):
git config --global user.email "your@email.com" git config --global user.name "Your Name"
where “your@email.com” and “Your Name” will be the same as the account email and the full name supplied while creating the Bitbucket account. The quotation marks need to be included.
To get the current tracking status of the files in the local (working) directory, the git status
command can be used:
git status
If this is the first time that Git has been initialised in this directory, the status will indicate that there are no tracked files and that there are no commits. To start tracking the files in the local (working) directory, the git add all
command can be used:
git add --all
or
git add .
The full stop at the end of the command tells Git that all the files need to be added. In Git, we also refer to this as staging. This step is always required before a commit (see below) can be made.
The git status
command can be used regularly to see which files are being tracked/staged and which ones are not.
Now that the files are set to be tracked (or staged), they can be committed. This is a required step and is done using the git commit
command.
Committing will add a manual desired message to all the files being committed (in this case it will be all of them), together with the date.
In the future, each file can have its own commit message. The commit message used is usually a short description of the changes made. Because this will be the first commit made, a simple message, e.g. “First commit”, can be used:
git commit -m "commit message"
where “commit message” is whatever you want it to be. It is usually anything that describes the changes you are committing. The commit message is written between double quotation marks.
By adding the -m
parameter to the command, it prevents Git from opening the default text editor to write a comprehensive commit description/message (recommended).
On Raspbian, the default text editor will most of the time be Nano. In some instances of Git, Vim will be used as the default editor. If you want to use the default text editor to write the commit message instead, the git commit
command is used in its simplest form as follows:
git commit
After all the files have been committed, we need to connect Git to the repository. We will use the ‘mycurrentproject’ repository created earlier. To connect Git to the repository, the git remote add origin
command is used:
git remote add origin https://yourusername@bitbucket.org/yourusername/mycurrentproject.git
where “yourusername” is the same as the username used when creating the Bitbucket account. “mycurrentproject” will also depend on the name of the repository given while creating it.
The final step in the process is to upload (‘push’) the files to the repository, but first, we need to see if it is linked correctly. This can be done using the git remote
command:
git remote -v
Finally, all that remains is to push the files from the local (working) directory to the Bitbucket repository. This is done using the git push origin master
command:
git push -u origin master
By refreshing your Bitbucket repository, all the files from the Raspberry Pi project (working) directory should now be present.
Now that the Bitbucket repository has been updated from the Raspberry Pi, the files are safely situated on a remote server. The file can now be tested, used or changed locally. See the Updating a Bitbucket repository from a Raspberry Pi section later to continue.
To exit the terminal, the exit
Bash command can be used:
exit
Cloning a Bitbucket repository to the Raspberry Pi
The previous section showed how to ‘push’ (upload) files from a Raspberry Pi to a Bitbucket repository. This section will show how to copy/download (‘pull’) a repository from Bitbucket to a Raspberry Pi using Bash and Git commands. The next sections will show how to update a Bitbucket repository from a Raspberry Pi and how to get the latest updates from Bitbucket.
To get started on the Raspberry Pi, the location where the repository will be saved needs to be established. The repository directory name will be the same as the repository name and will be created automatically.
Let’s say you want to copy the “mycurrentproject” repository to the home/pi/projects
directory. To create a directory, the following Bash make directory command can be used:
mkdir home/pi/projects
or while in the home/pi
directory, a new directory can be created using:
mkdir projects
Git will copy (‘clone’) files to the current working directory. To go to the home/pi/projects
directory, the change directory command is used:
cd home/pi/projects
To clear the terminal from all previous data, the clear
or cls
Bash commands can be used:
clear
While inside the desired parent directory, the git clone
command is used:
git clone https://yourusername@bitbucket.org/yourusername/mycurrentrepository.git
where “yourusername” is the same as the username used when creating the Bitbucket account. “mycurrentproject” will also depend on the name of the repository given while creating it.
Apart from creating the first copy of a repository, the git clone
command will also set up additional files needed for Git to recognise and the repository (similar to the git init
command).
The Git clone command to a specific repository will also be communicated at the top of each new repository created in Bitbucket. Similarly, repositories from other services, e.g. Github, can also be cloned. The account password will be required with private repositories.
Now that the repository has been copied/cloned to the Raspberry Pi, the files can be tested, used or changed locally. See the Updating a Bitbucket repository from a Raspberry Pi section below to continue.
To exit the terminal, the exit
Bash command can be used:
exit
Updating a Bitbucket repository from a Raspberry Pi
The previous sections showed how to upload existing project files from a Raspberry Pi to Bitbucket and how to clone a Bitbucket repository to a Raspberry Pi. This section will show how to update a Bitbucket repository from a Raspberry Pi. The next section will show how to get the latest updates from Bitbucket.
In this section, it is assumed that your Raspberry Pi project was either cloned from a Bitbucket repository or created on the Raspberry Pi and ‘pushed’ to Bitbucket. Either way, you will have a copy of your project on the Raspberry Pi’s working directory and as a Bitbucket repository.
Assuming that the Bitbucket repository files were not updated or changed since a git push
or git clone
command was used, the Git status would be clean before starting. This means that there are no files to commit (see later). It also means that no files are staged yet (see below). To make sure of this, the git status
command can be used:
git status
Remember that the Git status command can be used regularly to see what files were changed and what files were staged.
To clear the terminal from all previous data, the clear
or cls
Bash commands can be used:
clear
It is now time to make some changes. New files and directories can be created and/or changed inside the Raspberry Pi working directory. Once you’re happy with your first set of changes, the Git status command will show the new file changes made within the working directory.
Git can show the changes made by using the git diff
command:
git diff
which will show you all the changes that have been made. Similarly, the Git diff command can also be used on a single file:
git diff <filename>
where <filename> is the file you want to view the changes from (e.g. index.html
).
In some terminals, the Git diff command will not exit back to the terminal input section. This can be fixed by pressing the q key on the keyboard. As with the Git status command, the Git diff command can also be used regularly to see the changes made.
The next step, before the files can be committed and copied (‘pushed’) back onto the Bitbucket repository (see later), is to “stage” the changed files. Staging is an abstract concept used by Git which means that files are to be tracked.
It also makes a backup of the file (stored in the cache) for when experimental changes are to be made. Staging is a required step before files can be committed. To stage all the changed files at once, the git add all
command can be used:
git add --all
or
git add .
The full stop at the end of the command tells Git that all the files need to be added.
Singe files can also be staged using the git add
command:
git add <filename>
where <filename> is the file you want to stage (e.g. index.html
).
As mentioned, once a file is staged, experimental changes can be made to the file in the working directory. Because there is a staged version of that file, experimentation can be done without the fear of losing the initial copy of the file.
If you are unhappy with your experimentation, the staged file can be recalled by using the Git checkout command.
Let’s say the index.html
and the /css/styles.css
files were staged, and you were not happy with the experimental changes made afterwards. In this case, both the files (which are all the files) can be reverted using the git checkout
command as follows:
git checkout .
The full stop at the end of the command tells Git that all the files need to be reverted. If you are only unhappy with the /css/styles.css
file, only that file can be reverted by using the Git checkout command as follows:
git checkout --<filename>
where <filename> is the file will be /css/styles.css
.
Once you’re happy with your experimentation, the new files need to be staged, once again, using the Git add or the Git add all command. By staging the experimental files in the working directory, the old staged files will be overwritten.
In the case where the project has been cloned from the Bitbucket repository, you need to tell Git who you are by using the following two Git config commands (only required once per repository):
git config --global user.email "your@email.com" git config --global user.name "Your Name"
where “youe@email.com” and “Your Name” will be the same as the account email and the full name supplied while creating the Bitbucket account. The quotation marks need to be included. This step will not be required if the project were created on the Raspberry Pi and ‘pushed’ to Bitbucket.
The next step in updating files using Git is to commit the files. Only staged files can be committed. In Git, a commit, or “revision”, is an official change made to a file. It is like when you save a file, except with Git, every time you save it, it creates a unique ID (or “hash”) that allows you to keep a record of what changes were made when they were made and by who.
Each commit also has its own description (or commit message) which is set by the user. The commit message/description is used to write down what changes were made.
To make a commit of the staged files, the git commit
command is used:
git commit -m "commit message"
where the “commit message” is anything that describes the changes you are committing. The commit message is written between double quotation marks.
By adding the -m
parameter to the command, it prevents Git from opening the default text editor to write a comprehensive commit description/message (recommended).
On Raspbian, the default text editor will most of the time be Nano. In some instances of Git, Vim will be used as the default editor. If you want to use the default text editor to write the commit message instead, the Git commit command is used in its simplest form as follows:
git commit
To make sure that the commit was successful, the git log
command can be used:
git log
Then finally to upload the files to the Bitbucket repository, the git push
command is used:
git push
The account password will be required with private repositories.
To exit the terminal, the exit
Bash command can be used:
exit
Getting the latest update from a Bitbucket repository
The previous sections showed how to upload existing project files from a Raspberry Pi to Bitbucket, how to clone a Bitbucket repository to a Raspberry Pi and to update a Bitbucket repository from a Raspberry Pi. The last section of this post will show how to get the latest updates from Bitbucket.
In the case where the Bitbucket repository files were updated (e.g. from another Raspberry Pi), the older files will still be in Raspberry Pi’s local (working) directory. This means that the files on the Raspberry Pi will need to be updated to their latest versions. For this, the Git pull command is used.
The pull command can only be used after the Git clone command (see earlier) or if the project was created on the Raspberry Pi and ‘pushed’ to Bitbucket. While inside the working directory, the git pull
command can be used as follows:
git pull
This command will update and merge the remote changes.
To exit the terminal, the exit
Bash command can be used:
exit
Conclusion
Git is a great way to keep track of and experiment with programming projects on the Raspberry Pi. In combination with Bitbucket repositories, Git can also be used to store projects in a remote location, which also serves as a cloud backup of your work.
By using the Git commands mentioned in this post, we showed how to upload existing project files from a Raspberry Pi to Bitbucket, clone a Bitbucket repository to a Raspberry Pi, update a Bitbucket repository from a Raspberry Pi and how to get the latest updates from Bitbucket.