CS 222

Logo

Programming Languages

Lab 1.5 - Environment setup

You’ll need:

Git is a distributed version control system (VCS), originally built (in a single weekend in 2005) by Linus Torvalds to manage the development of the Linux kernel. It’s come a long way since then. According to a recent study by Synopsy, it’s the most widely used VCS; 70% of software repositories are Git repositories. The command-line tool git allows you to interact with a Git repository.

GitHub is a service which hosts Git repositories and provides a web interface for development workflow such as issue tracking, branch management, pull requests, continuous integration, software releases & downloads, hosting websites via GitHub pages, and greater support for collaboration. All repositories on GitHub are Git repositories (although some mirror SVN repositories or provide support for other types of repositories), but not all Git repositories are hosted on GitHub (but many are!). As of January 2020, GitHub is the largest host of source code in the world, hosting over 100 million repositories by 40 million different users.

GitHub has also recently developed the GitHub Desktop Client, a graphical user interface (GUI) that allows you to interact with a Git repository without using the command-line. In this class, we’ll use GitHub as a Git repository hosting service and we’ll use GitHub Desktop to interact with the Git repository which will be hosted on GitHub and also saved locally on your machine.

Step 0. Install an editor of your choice

I’ll primarily use Visual Studio Code in class, but you can use whichever editor is your favorite. If you’re not sure where to start, Atom is also free and pretty popular. Sublime Text is great, too. There’s a free version, or you can pay to support the developer.

Step 1. Install GitHub Desktop Client

Mac/Windows users, install GitHub Desktop Client. Linux users, follow the installation instructions for GitHub Desktop for Linux.

Open GitHub Desktop. Sign in with your GitHub username/password, or create your free account if you don’t have one already. You’ll need to set a few configuration settings:

  1. Select ‘Preferences –> Integrations –> External Editor’. Pick your favorite text editor.
  2. Select the file menu option ‘GitHub Desktop –> Preferences –> Git’. Type in your name and the email address associated with your GitHub account.

Step 2. Install Git

Follow the Git installation instructions. To check if you already have it installed, or to check if the install was successful, run the following on the command line. It should output your current version of the command line client.

git --version

Step 3. Install Java SE (Java Development Kit JDK)

Follow the Java SE/JDK installation instructions. To check if you already have it installed, or to check if the install was successful, run the following command line. It should output your current version of the java compiler.

javac --version

Step 4. Hello world

In the Teams ‘Announcements’ channel, I posted another link to GitHub classroom. Follow that link to find the starter project called java-hello-world. Sign into GitHub if prompted. Select your name from the list and continue. In a few moments, it will create your own personal copy of this project. Once it finishes, click the link to continue to your project. Keep this tab open on your web browser because we’ll come back to it in a moment.

Getting the project with starter code onto your computer

Clone this project to your computer and run it with the following steps:

  1. On the web browser, click the green “Code” button. Select “Open with GitHub Desktop”.
  2. In GitHub Desktop, click the button “Open in <Your Preferred Text Editor>”. This will open the project in your favorite text editor. You should see three files: Main.java, README.md, and .gitignore (this file tells Git which files it shouldn’t care about tracking). Open Main.java to read it. This is a working program, so we’ll run it next.
  3. In GitHub Desktop, click the menu button “Repository –> Open in Terminal / Command Prompt”. This will launch your command line tool and change directories so that the current working directory is where the git software repository java-hello-world is located.
  4. In the command line window just opened, type ls and enter to list out the files in the current working directory. You should see the files Main.java and README.md.
  5. You should see the following after running these commands. The first command compiles the Java program. The second command runs it.
    javac Main.java
    java Main.java
    

    You should see “Hello world!” output on your command line. You now have a working Java program! 🙂

Making a change and saving it locally

In your text editor, change the line of code so that it reads “Hello, <Your Name>” instead of “Hello, world”. Test that it works by recompiling and rerunning the program.

The change you made is local only to your machine. It isn’t yet on the Git repository which is hosted on GitHub. As a version control system, Git will keep track of what changes have been made to this repository. You have to explicitly tell it which changes are important, that is, which changes you’ve already made on your machine that you want to be reflected and ‘saved’ in your local repository. This ‘local save’ is accomplished by adding and committing the file with changes to the master branch. The next few steps will walk you through committing your changes to your local repository.

  1. In the GitHub Desktop window, you should see that 1 file has changed in the java-hello-world project. It will give you a colorized diff of those changes.
  2. In the bottom left, add a descriptive commit message such as “Print out my name”. Click ‘Commit to master’.

Each commit is a snapshot of the current state of the project. Note that your changes are still only local to your machine at this moment. This step allows you to make multiple changes to your repository, save those changes locally with separate commits such as “Fix bug 1”, “Fix bug 2”, etc., as you progress. This way, you don’t have to be connected to the internet to save your changes in a logical and cohesive manner.

Side note: the master branch is the default branch, typically preserved for the main / stable project state. A typical work flow may include multiple branches, such as ones for specific bugs like the branch bug1093, one such as develop for the latest copy of the project which hasn’t been released yet, etc. Branches can have any name, can be worked on simultaneously, and can be merged later on. It allows for parallel software development. For example, Ariel can work on fixing a bug on the branch bug1093, while Christian and Marion can work developing a new feature in the branch feature, in such a way that changes to one branch won’t affect the other branch. Once the work in one branch is complete, it can be merged into the main stable branch. We won’t do much branching/merging for these projects, but this concept allows for easier collaborative development when working with a large group.

Pushing your changes to the repository on GitHub

In order to upload these changes to the remote repository hosted on GitHub, you’ll do a push operation. In the GitHub Desktop window, you’ll see highglihted in blue ‘Push commits to the origin remote’. Click the button ‘Push origin’. You’ll need to be connected to the internet for this part.

Next, refresh the tab open in your web browser that had your copy of the project java-hello-world displayed. Here’s how you can check that your changes were correctly submitted. In blue at the top, it will show the latest commit to the master branch. You can see all commits in the history of the project by clicking the link ‘X commits’, where X is a number. You can even click on any commit (the latest one at the top is of most interest) to see what changed.

If you can see your changes on your web browser, then that means I can see your changes as well. This is how you’ll submit your coding projects. Each coding project will have a separate repository which you can download locally, work on in stages (solve one problem, save and commit, solve another problem, save and commit, etc.), and then submit by pushing it to GitHub. The more descriptive your commit messages are, the easier it will be for you to remember what work you did and where you left off. Make it a standard to only ever commit code which compiles. You can also push multiple times, as I’ll only download the current copy of your project from GitHub after the due date for the project.

Note: Free stuff

I’ll provide the private repositories to you for all our class projects. But if you want to use Git/GitHub outside this class, as a student, you have unlimited personal private repositories. You may have to verify with GitHub your student status with your Wooster email / student ID. It’s not needed for this class, but you also have access to the GitHub Student Developer Pack. Additionally, I have extra Octocat stickers (the GitHub mascot), so let me know if you’re interested in one for your laptop!