Git for Class

Dr. Greg Bernstein

August 5th, 2019

Git for Class Work and Small Projects

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

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.

Why for Class Assignments?

  • Websites and non-trivial programming projects are composed of many files and directories

  • Learning Management Systems (e.g. Blackboard) are not oriented towards multi-file assignment submissions and may not permit file types that are key 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

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 a folder.

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
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.
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 you project directory. Do not accidentally 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

Git file states and workflow.
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…
  • Many editors include git support or plugins: Atom, Brackets, etc…
  • Other tools I use: Tortoise Git
  • I always go back to the command line if things get complicated or confusing!

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

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
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 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
You, git, and GitHub

Local and Remote Repos

Contain roughly the same information, remote can be shared

Basic Remote Commands

  • clone a remote repository to start a new project
  • 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 you can make frequent use of them as the Tutorial section will demonstrate.

When to Branch

  • 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.

Branch Visualizer

Git Visualizer. “Explore how Git commands affect the structure of a repository within your web browser with a free explore mode, and some constructed scenarios.”

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.

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.

Command Line Tutorial

Personal Git Tutorial. About the minimum you need to know to use Git locally for personal projects.