Phil-duino

A collection of Arduino tutorials put together to help a colleague dive into embedded systems!

View the Project on GitHub ChrisAlphabet/Phil-duino

Git

Phil, this is the number one thing that you have been asking for. I had thought to introduce this at the end so you wouldn’t get bogged down by some of the infrastructure that surrounds writing code, but apparently you know better than I do. So, lets wade into the muck.

What is Git?

Git is a version control system that is used to track changes made to files. Someone will be raging about ‘content not files’, but they are poopy-heads. Git stands for ‘Gross Itchy Testicles’. Ok, that was a lie. It doesn’t stand for anything. To quote Linus Torvalds,

I’m an egotistical bastard, and I name all my projects after myself. First ‘Linux’, now ‘Git’.

There are other version control systems, but Git has won the war. Don’t waste your time learning about anything else.

How does Git work?

Fucked if I know. I am sure you can find out about it somewhere, but that’s not what we are about. We will learn how to use Git, the same way a pianist (he he, penis) learns how to create music, not how to make pianos.

Command Line or Graphical User Interface?

To begin with, we will use the command line. Once you understand the git workflow, you can swap over to a GUI. We use Source Tree at work, I use Git Kracken at home. The advantage of Git Kracken is that it works on Linux aswell. Use anything else you like, but I won’t support your decision.

The Basics

Git is a distributed version control system. Something something peer-to-peer something something client-server. Go read the wikipedia page yourself. Basically, there is a central repository and local repositories. The central repository is typically called ‘master’. You clone ‘remote master’ to get a local repository with a ‘local master’. From here you can do whatever you like, in complete isolation to the ‘remote master.’ You can, of course, pull the remote master into your branch and push your altered master at any point. So you aren’t completely isolated, but you get the point. If you you fuck up your ‘local master’, you haven’t automatically fucked up the ‘remote master’ and ‘every other local master’. And that is beautiful, because you will inevitably fuck up but now you have a safety net.

So here are my typical steps when using Git:

  1. Create a repository on Github
  2. Clone the repository onto your machine
  3. Create a branch for a particular feature
  4. Code, add, commit, push
  5. Repeat 4 until particular feature is complete
  6. Create a pull request to have particular feature added to master
  7. Approve your own pull request that merges particular feature into master, because you have no friends to look at your code.
  8. Pull the updated master into your remote master
  9. ???
  10. Profit!

So, lets do it!

Losing your Git virginity

‘Phil, I promise this won’t hurt a bit’, I whisper seductively into your ear while fumbling with your bra strap…

Setup your machine

Download Git for Windows.
Open a command prompt and type git --version. If that works, good. If not, make it work. I like to use ‘Git Bash’ when on Windows, you should have it now as well.

Clone a repository

Did you know that this poorly written tutorial series is available on Github? That seems like a great place to start! Lets clone it to your desktop.

Open a Git Bash shell.
Change into the Desktop directory:
cd Desktop
Clone the Phil-duino repository: git clone https://github.com/ChrisAlphabet/Phil-duino.git
Change into the repository: cd Phil-duino
The shell should now show that you are on branch master! Well done!
If you list the files using ls you should see something like:

docs/ 
fritzing/
README.md
solutions/

Make a branch

The feature that we are going to add is a new directory where your homework exercise will live. Isn’t that exciting?!

To view all the branches, use: git branch

You should see that the only branch you have is called master with an asterisks next to it. The asterisks tell you what branch you are currently on.

So, lets create a branch called homework-directory: git branch homework-directory

View all the branches again and you will see a new branch has been added. But the asterisks is still next to master?! That is because you haven’t checked the branch out.
git checkout homework-directory

View all the branches again and finally you are on your very first branch!

View the status of the branch and you will see that you are on branch homework-directory and have nothing to commit: git status

Wonderful, now we can get down to business!

Add a file to the repository

Create a new directory called homework-exercises:
mkdir homework-exercises

Change into the new directory: cd homework-exercises

Create a file and put some text in it: echo "# My first branch" > hello-git.md

Now, check the status and what do you see? You now have untracked files! That sounds a bit dangerous, better start tracking those files. We need to add them to the staging area.

git add -A

where -A stands for all.

Guess what..? Check the status! You now have changes to commit

Commit the file

Imagine you are playing Halo and you are just about to single handedly take on two Hunters with no ammo. Wouldn’t it be nice if there was a checkpoint you could go back to when they inevitably smack you to the other side of the fucking map? Well, writing code is the same. It is nice to have checkpoints we can go back to when we inevitably butcher our code base so hard that Kel Knight tries to sell it in sausage form.

We can build up checkpoints, or commits, on our machine locally and then push them to the central repository.

git commit

Ahh shit! You are deep in it now! This is called vim, and it, along with emacs form one of the great holy wars of keyboard warriors. Quick! Type a message, like ‘git gud.’ Press escape. Type :wq and get the fuck out of there! Arghhhhhh!

Phew, that was close. You can directly add a message using:

git commit -m "git gud"

Now we have checkpoints that we can roll back to if everything goes to shit. Yay!

Push the changes to the central repository

Lets say we are happy with the changes we have made and would like to make them available to everyone else, whether that be future-Phil on a different machine or Phil traveling around the country on his adventure bike. We need to push them to the central repository. Which is as simple as:

git push origin homework-directory

where origin is an alias on your system for a particular remote repository, specifically a URL. View it using: git remote -v

Note: shamelessly pillaged from Stack Overflow

You might need to enter your Github credentials.

Create a pull request

This part isn’t very exciting. Go to Github and follow the prompts to create a pull request.

And that’s it! The basics of Git.

Homework Exercises

  1. Follow the steps from the tutorial to create a branch named ‘phils-tutorials’, create a directory in solutions for each tutorial, and in each tutorial directory put your Platformio project for each part.
  2. Commit and push changes to the remote repository.
  3. Create a pull request to get the changes added to master.
  4. Make changes and experiment with git reset. What does it do?