Using GitHub

As you start to create advanced projects on the Raspberry Pi, you’ll need more advanced code. You could type it in by hand and often you’ll find code on developer’s websites but increasingly it’s being housed on Github.

GETTING TO KNOW GIT

Git is online versioning software that enables you to store code online, download code, make changes and then integrate those changes. It enables many people to work together on software. Github (github.com) is a website that developers use to host links to the code.

STEP 1

Git enables you to clone code created by other developers on your Raspberry Pi. So it’s a vital tool to have on your Raspberry Pi. You install Git using APT. First enter sudo apt-get upgrade to get the latest package lists and sudo apt-get install git to install the latest version of Git.

STEP 2

Enter git –version to make sure it’s installed correctly. You should have the latest version installed (ours is 1.7.10.4). You can enter git –h to get quick help or man git to read the manual but Git is pretty complex at first so it’s better to follow our guide. We’re going to use the desktop to take a look at Github next, enter startx.

STEP 3

Open the Epiphany web browser and enter github.com. Click Sign Up and create an account. Now open a terminal window and enter git config --global user.name “John Doe (replacing John Doe with your name). Next enter git config --global user.email john.doe@mail.com replacing the email address with the same one you used to sign up to Github.

STEP 4

Now we’re going to clone a project from Github to our computer. In the Epiphany web browser enter github.com/lucyhattersley/fizzbuzz. This is a repository I’ve created on git that contains a program called FizzBuzz. A repository is like an online folder that contains the code. You will see links to the files contained in the project: README. md and fizzbuzz.py but don’t click these.

STEP 5

In the bottom right of the Github window you will see a text box marked HTTPS clone URL and inside it is contained a URL. Click the text and press Control-A to select it all. Now right-click and choose Copy. Now open a terminal window and enter git clone and choose Edit > Paste to paste the HTTP address after the command.

STEP 6

Enter ls and you will see the Fizzbuzz directory in your home directory. Enter cd fizzbuzz to switch that directory and ls again to view the fizzbuzz.py and README.md files. FizzBuzz is a classic program that counts to 100 but on numbers devisable by 5 or 3 it prints Fizz or Buzz instead (or FizzBuzz if it’s devisable by both 5 and 3). Enter python fizzbuzz.py to run the code.

ADDING CODE TO GIT

Now that you know how to get clone code from Github on your Raspberry Pi, you might be wondering how to put your own code into Git. Git is a powerful and fairly complex piece of software but this will help you get started.

STEP 1

We’re going to create a Python program that reverses a string and upload it to Git. Enter mkdir reverse_string and cd reverse_string. Then nano reverse_string.py and enter this code:

import sys                                                                           
string = sys.argv[1]                                                                 
print string[::-1] 
Press Control-O and press Enter to save the code. Press Control-X to exit.

STEP 2

You can test the program by entering python reverse_ string “Hello World You should see dlorW olleH returned on the command line. We’re going to put his program on your Github but first it needs to be a Git folder, enter git init. This initialises the directory as a git repository. It creates a hidden file called .git to the folder; you can see this using ls –lah.

STEP 3

First we need a repository. Open Github (github.com) and click the green New Repository button. Enter reverse string in the Repository name field and select the Initialize this repository with a README option. Click Create repository to make the file. Next you need to copy the text from the HTTPs clone URL field as you did when cloning a website. Return to the command line and enter git remote add and use Edit > Paste to paste in the URL.

STEP 4

You’ve now linked the local directory to the Git repository and can sync changes from one place to another. Enter git add reverse_string.py to track the python file. Now enter git commit -m “First commit to tell Git you want to send (commit) the file to your Git space. Finally enter git push and enter your user and password to send the files. You’ll now see them on github.com. This is only a really rough introduction to Git. Head over to codeschool.com/courses/try-git to find an online course.

More information