RTC Website

Setting up your workspace as a Git repository

DX

This section will guide you through the process of initializing your workspace as a Git repository and the usage of the basic Git commands.

The terminal

In your development system virtual machine (VM), open a terminal window by clicking and typing bash. Select the Git Bash app that appears. You should see a terminal window with a command prompt, such as

vagrant@DESKTOP-JVPS25H MINGW64 ~
$

This means that you can execute a command or program name in the current working directory /Users/vagrant by typing it in and hitting keyboard_return. Enter the command ls. This lists the directories and files in the current directory.

Change the current directory to the workspace with the cd command:

cd /z/workspace

Listing the contents with ls displays the contents that we considered in figure 0.8.

Initializing your workspace as a Git project

You can manage your Eclipse workspace with Git, which is already installed on the VM. Continuing in the terminal, with the workspace as the current directory, we can initialize your workspace as a Git project with

git init

This creates a .git subdirectory where Git stores your version control information. We must tell Git who we are, for posterity:

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

There are three primary sections of a Git project, shown in figure C.1:

Working tree

The files we are working with in workspace

Staging area

The location where we prepare or “stage” files and directories to be committed to the repository

Repository

The location where Git keeps track of the files and the history of commits

 Figure C.1
Figure C.1: The three sections of a Git project and the git commands that move files among them.

We can check to see the status of the Git project with

git status

We can see that none of our files are “tracked” (i.e., their changes stored in the Git repository) because they have not yet been added to the repository. We want to track everything but the .metadata directory, which contains Eclipse settings that are tedious to maintain in version control. Therefore, we tell Git to ignore this directory by creating a .gitignore text file and simultaneously adding the text .metadata to its content:

echo .metadata >> .gitignore

Now git status lists the new .gitignore and not .metadata,1 and we are ready to have git start tracking the rest of the workspace. Individual files can be staged for commit to the staging directory with git add <filename>, but we would like to add all files:

git add -A

A git status will list the files new to git. At this point, they are considered staged for a commit to the “memory” of the Git project, the repository. To commit,

git commit -m 'initial commit'

The -m option tells Git that a message ('initial commit' in this case) is about to follow. One should be as thorough and succinct as possible in a commit message.2 Your workspace has now been committed to the Git repository and is ready for code development.

Updating the Git repository

Say that we have made some changes to the workspace, as we did in subsection 0.2.1 for the hello-world project, and we want to commit these to the Git repository. Back in the Git Bash terminal, with ~/workspace as the current directory (cd ~/workspace), run

git status

The output will show one of the modified files is hello-world/main.c, which we modified in subsection 0.2.1. Untracked is the new hello-world/Debug/ directory, which now has hello-world/Debug/main.o and hello-world/Debug/hello-world.exe. Stage both hello-world/main.c and hello-world/Debug/ with

git add hello-world

A git status shows that these files are staged for commit. Commit them to the repository with

git commit -m 'customized print, built, and executed successfully'

In some projects, you may not want to track executable files like main.o and hello-world.exe because they are generated by the source files and contain only binary data. Git cannot track differences in binary files, so if they are tracked, the entire file is updated every time there is a new commit. You can exclude them from tracking by adding them to .gitignore.


  1. The file .gitignore is just a text file. Additional lines can be added to ignore more files. The >> operator will append to an existing file.↩︎

  2. If one executes git commit without the -m option, it will open in the Vim editor within the terminal window. Don’t panic. Usually, it will already be in INSERT mode and you can type your message. If typing does nothing, hit the esc and then i to enter INSERT mode. After entering your message, save it with esc, :w, keyboard_return and then quit with esc, :q, keyboard_return.↩︎

Online Resources for Section C.1

No online resources.