Developing in C for the ATmega328P: Using git

5 minute read

Where I discuss how to implement and use git to develop code in C for the Arduino Uno.

Sources

Introduction

In this post, I’ll describe how to implement git on your OS (Linux, macOS and Windows) and how to use it to develop code. Its helpful as a personal tool and it becomes mandatory, once you begin using someone’s git repository.

I am not a git expert and I’ll try not to stray too far from status->add->commit-push

Big Picture

Git is a version management application. It allows you to make incremental changes in your code, saving and commenting those changes then providing the capability to roll-back your changes should you desire. It is also the version management system used by software professionals to manage extremely large software deployments with teams measuring in the thousands. I’m describing git from the former example…

An Overview of the the git Process

The process of using git is the process of supporting version tracking in code development. For example, you have a small application consisting of two files called main.c and main.h. The code has been working, however, you want to make some substantial changes. Here is an overview so you can see a simple git process:

  1. The directory for the two files is called myApp so you cd into myApp:
cd myApp
  1. You initialize the directory to be tracked by git:
git init
Initialized empty Git repository in /Users/lkoepsel/Documents/myApp/.git/
  1. You make changes to the files and to confirm those changes have been seen
git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	main.c
	main.h

nothing added to commit but untracked files present (use "git add" to track)
  1. You add the files to be “staged”. (At present we’ll ignore this concept, it simply means the two files haven’t been added to the repository yet. So let’s add them.) -A means, all changed files.
git add -A
  1. This is the big step, now let’s commit them so that the repository automatically creates a new version of the code.
git commit -m "This is my first commit"
[main (root-commit) 2daa3fb] This is my first commit
 2 files changed, 2 insertions(+)
 create mode 100644 main.c
 create mode 100644 main.h

This is the process of tracking your code, and you perform these steps as often as you want. Too often and it becomes tedious, however, not enough and you will rue the day. (Speaking from experience.)

git with others

There is another reason to use git and that is to easily use other people’s software projects. This will be our first usage of git, we will use git to download a repository of the Lab software. The value of using git in this process is that if a student finds an error in the instructions or examples, I can easily update the repository and each student can easily update their repository as well. This will require each student to ensure they don’t make changes to the examples or lab documents.

Step 1: Get git

Linux (and Windows Subsystem for Linux)

For Linux systems, git is probably already installed. To test that:

git --version

If you don’t get a message showing git and its version, it isn’t installed. To install use:

sudo apt install git

Once installed, perform the git –version command in the command line.

macOS

For macOS systems, git probably won’t be installed. To test that:

git --version

If you don’t get a message showing git and its version, it isn’t installed. To install use: (*Note: If you don’t have homebrew already installed, see this )

brew install git

Once installed, perform the git –version command in the command line.

Windows

Windows doesn’t have a standardized Unix/Linux environment, unless you’ve installed WSL. In this case, we’ll install Git for Windows. Click on Download and save the file to your Downloads directory. Install Git for Windows by double-clicking on the downloaded file. It will ask a series of questions and for now go with the defaults.

Once installed, go to the Windows search bar and enter git , which will start git in the command line. Enter:

git --version

to confirm git was installed.

Step 2: Get the Lab Content

In this step, you will use git to download the Lab repository. It is important to
follow the next steps exactly, as git will make certain assumptions about the directories in the repository. A mis-spelled directory could create a lot of work, later on.

  1. Open a command line terminal such as Windows Terminal in Windows, Konsole in Linux or Terminal in macOS. Change directories your main documents directory, typically called Documents:
cd Documents
  1. In your browser, go to the Labs github page. Click on the 1) green code button, make sure 2) HTTPS is in bold and has a red line under it and click on the end of the line with the 3) two over lapping boxes sit (copy icon). This will copy the name of the repository into your buffer.
    Github Page for the 10C Labs

    Github Page for the 10C Labs

    Large Version to see detail
  2. In the command line, type “git clone " (make sure there is a space after clone) then right-click and select Paste. You will have the following:
git clone https://github.com/lkoepsel/Labs_10C_Class.git

Press return and git will create a directory called Labs_10C_Class in your Documents directory. 4. Now go into the Labs directory:

cd Labs_10C_Class
  1. Create your development directory called dev, at the same level as examples, templates and Library by duplicating templates. This will serve as your development directory and is ignored by git. Doing all of your work in this directory is very important as changes made in other folders will cause git to fail if an update is needed.
cp -r templates/ dev

You are now ready to work! Your next step will be to add the tool chain for your specific operaing system. Go to Setup Introduction.

Comments powered by Talkyard.