Git for Class and Small Projects

Dr. Greg Bernstein

August 6th, 2021

Git for Class Work and Small Projects

Learning Objects

  1. Understand why you would want to use git on small projects.
  2. Apply git to personal and class projects in various development scenarios
  3. Understand the purpose of and differences between local and remote repositories
  4. Understand the difference between git and GitHub

References

  1. Git Book: Getting Started

  2. Git Book: Git Basics, skip 2.6 tagging and 2.7 aliases.

  3. Git Book: Branching, Section 3.1 only. We will never merge, we will never do “pull requests” etc…

VCS/SCM

Git is a version control system (VCS), also known as a source code management (SCM) system. A VCS allows you to track changes to your code over time in such a way that previous versions are always available in case you have a need to go back. In larger projects VCS is very important in coordinating the work of multiple developers.

Version Control in General

  • Software version control is a huge topic
  • We will only cover a minimal subset needed for this class
  • Classes on software engineering would cover this in more depth

Poll: Version Control Experience

What experience, if any, have you had with a version control system?

Personal Projects

Git has properties that make it quite amenable to this use:

  1. git runs locally, no need for network access to use it on your own projects,

  2. git works on a per project basis, no elaborate configuration and simple per project initialization.

  3. git is completely free.

Simpler Alternative?

After each significant change to a file save a copy of it with a “special” name, i.e., suffix or prefix.

Issues with this approach:

  • Where to keep all these copies without making a mess out of your project directory
  • What happens when you have many files in a project (common in web development)

Why for Class Assignments?

  • Websites/Apps and programming projects can be composed of many files and sub-directories

  • Projects extend over many weeks via assignments that are due weekly

  • Learning Management Systems (e.g. Blackboard) are not oriented towards multi-file assignment submissions and may not permit file types that are important to this class such as .css, .js, .html!

  • Need a relatively easy, time-stamped, and private way for you share your work with instructor(s).

Minimal Git Concepts and Commands

Project Directory

  • For organizational purposes we keep all our project files together in a directory
  • This project directory can contain sub-directories to further organize your files
  • git will work with this project directory!

Project Directory Example 1

CS Oriented Audience Response System

CSOARS project directory structure

Project Directory Example 2

Lots of small project directories for class examples

Course examples project directories

Tips to Avoid Issues

  1. Do not use spaces or other weird special characters (?, #, !) in your file/directory names
    • Fix these before committing them
    • Screen capture programs sometimes do this to you
  2. Do not have files/directories in the same directory that only differ in their capitalizations
    • Particularly a problem with Windows machines

Repository

  • Storage for the entire file history of your project.
  • A project can have multiple repositories (git will help you keep them aligned)
  • Can be seen on disk as a .git directory within the project directory

Working Files

  • These files and directories are what you directly modify
  • You never directly modify the .git directory/files
  • You use git commands to commit working file changes to the repository or to checkout working files from a previous time/branch.

Local View

Local View

Development Snapshots

Git allows you to take and keep permanent “snapshots” of you project at any point in time via an action called a commit. This is represented in the figure below from Git-Basics.

A snapshots of project history via git.

Where is the History?

All these snapshots are saved in a very efficient form in the git repository. The git repository is simply a directory .git that Git will maintain within your project directory. Do not delete this directory!

Workflow

As the pro-git book (free) states:

  1. You modify files in your working directory.
  2. You stage the files, adding snapshots of them to your staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Workflow (Diagram)

Git file states and workflow.

Getting Status

You can always ask git about the status of your project, i.e., files in your working directory. Files can be in the following states:

Tracked

  • Tracked: Files that were in the last snapshot
    • unmodified
    • modified
    • staged

Untracked

  • Untracked: Anything in your working directory that was not in the last snapshot, has not been staged, or has not been ignored

GUIs for Git

  • Most IDEs include some type of git support! PyCharm, Visual Studio Code, etc…
  • There are many GUIs available. A good free cross platform one is GitHub Desktop
  • Other tools I use: Tortoise Git
  • I always go back to the command line if things get complicated or confusing!

Check Our Understanding

Interactive Questions

  • Can you go back to any point in time on your project?
  • What Happens if you delete the .git directory?
  • Do you need network access to work with git?

Remotes and Sharing

What is a Remote

From Git Basics – Working with Remotes

Remote repositories are versions of your project that are hosted on the Internet or network somewhere. You can have several of them, each of which generally is either read-only or read/write for you. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work

Why Do We Need a Remote for Class?

  • In this class you will be working individually not in project teams
  • A remote repository will be used to share your “code” with the instructor and grader
  • All assignments, projects, and exams are submitted via the remote repository

Who Hosts a Remote Repository

  • Anyone with a server on the internet can host remote git repositories.
  • You need git server software and this ranges from minimal command line server with a simple web interface to extensive web based GUIs and lots of features. See for example run your own git server

My Remote Git Server

Grotto Networking Git Server Private

Open Source Projects with Nice Git Hosting

GitHub

  • GitHub is a commercial company that offers free public repositories.
  • GitHub public repositories are used in some of the biggest and most popular open source projects.
  • Anyone can join and create free public repositories with a free GitHub account
  • They make money providing private repositories to individuals and organizations

GitHub Class Room

  • GitHub gave us a grant to use their Class Room service
  • This allows us to have private repositories for students shared with Professor and TA.
  • Underneath all the features there is a git remote!

Network View

You, git, and GitHub

Local and Remote Repos

Contain roughly the same information, remote can be shared

Local and Remote Repos

Basic Remote Commands

  • clone a existing remote repository to start work
  • push changes (commits) from your local repository to a remote
  • pull changes (commits) from a remote repository to your local

Workflow with Remotes

  1. You modify files in your working directory.
  2. You stage the files, adding snapshots of them to your staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
  4. You push your changes to the remote repo

Branches

Development Lines

The concepts so far allow for a single “line” of development, however the power of git comes from its ability to branch off a new line of development from any commit point.

Branches are Inexpensive

This is what allows you to go back to a previous “snapshot” in time of your project and start working from that point (you set up a new branch).

Branches in Git are inexpensive computationally and storage wise (this is not true of all VCSs), so we will use a new one for each assignment.

When to Branch

For personal projects

  • I set up a new branch either because I’ve messed up and need to start work from an old commit point, or I’m about to venture into the unknown, i.e., use a new library or framework, work on a new piece of functionality that I’m uncertain about.

  • You can always add, delete, or rename branches. Note that combining branches requires merging which we won’t cover here. See Basic Branching and Merging.

Class Use of Branches 1

  • In this class we will be building web site, web app, and web server projects
  • We will use good software engineering practice and have a partially working project every week corresponding to our weekly assignments
  • Each assignment will get a new branch so you can build on what you have done and I/grader will grade your work on the previous branch while you start on the new assignment.

Class Use of Branches 2

  • Branch names will be given to you and the exact name must be used so the grading tools I’ve written can find your work, wrong name ==> no grade.

  • All commits are get a secure hash and time stamp. We know when work was committed and will not grade late work (assignments, projects, exams)

  • You may commit and push as often as you like.

Check Your Understanding

  • When would you want to use a git remote?
  • What happens if you commit but don’t push?

Remarks

  • Git works best with text files rather than binary files, though it can handle both.

  • I take “snapshot”, i.e., commit my work rather frequently, especially after I’ve gotten something to work! Hence multiple times a day would not be unusual.

  • When solving homework assignments I typically commit when I finish each problem or sub-problem.

Getting Started

Two Approaches

  • When working for a company or on open source projects you typically start by cloning a remote repository. This is the approach we will use for our class repositories as explained in the Git for Class slides.

  • For a personal project that you may not share you can just use git init in your project directory as explained in my Personal Git Tutorial.

Git Command Summary

Init, Add, Commit

Name Description
git init Put a project directory under version control
git status Lists the state of files in the working directory. The -s option produces a shorter output.
git add files Adds files to the staging area. Common file wild cards are supported.
git commit Commits staged files.

Commit 2

Name Description
git commit -a Stages and commits all modified tracked files.
git commit -m “message…” Use the -m option with a double quoted string to set a commit message on the command line.
git mv Used to rename a file in git and in your directory.

Recover from recent goofs or visit the past

Name Description
git checkout fileName Puts the file fileName back to its condition after the last commit.
git reset --hard Puts all the tracked project files back to their state at the last commit.

Recover from recent goofs or visit the past 2

Name Description
git log Used to view the commit (snapshot) history.
git checkout commitHash Sets your tracked files to their state at a particular commit. Good for looking but don’t work on these files, set up a new branch for that.

Working with Branches

Name Description
git branch List the names of all the available branches.
git branch -v Lists all available branches and some info on their last commits.

Working with Branches 2

Name Description
git branch branchName commitHash Creates a new branch called branchName at the commit point indicated by commitHash.
git branch branchName Creates a new branch called branchName at the current “commit”.

Working with Branches 3

Name Description
git checkout branchName Checks out the branch branchName into the workspace.
git checkout -b branchName Creates a new branch called branchName from the current commit and checks it out to the workspace.
git branch -m oldBranchName newBranchName Use this to rename your branches.
// reveal.js plugins