Git Tutorial

Kyle Rogers & Nate Mara

2014-10-22

Intro

Git is a free and open source version control tool.

A Brief History of Git

In 2002, the Linux source code began to be managed by BitKeeper (proprietary software).

BitKeeper revoked license for Linux in 2005.

Requirements

  • Distributed
  • Performs efficiently
  • What goes in is what comes out

Git is Born

I'm an egotistical bastard, and I name all my projects after myself. First Linux, now git.

-- Linus Torvalds

Linus developed Git during a two week coding session. After two months, version 1.0 was released.

Since 2005, has developed into one of the largest source code management tools.

Basic Terms

Repository

A data structure, that contains, among other things a historical record of changes in the repository.

Commit

A snapshot of your repository at a specific time.

Branch

Set of commits, generally used for a single purpose. Branches can be combined with git merge

Basic Git Workflow

  1. Create repository
  2. Clone repository to local system
  3. Make changes to files
  4. Commit changes to git repository
  5. Push commits to remote

Picture of File System

image

Git Commands

git init

Create a new, empty repository in the current directory

git add <file>

Add <file> to the staging area. Files in the staging area will be added to the next commit.

git commit -m '<message>'

Commit files in the staging area. <message> should be a message that explains the changes made in the commit.

git help <command>

Get help for the given command

More Git Commands

git checkout <branch>

Switch to the branch identified by <branch>. To create a new branch with this command, add the -b flag.

git checkout <file>

When a file on disk differs from the version stored in Git, use this to revert <file> to the state stored in Git.

git status
View the current status of the repository. Shows:
  • current branch
  • modified files
  • and more!

First Time Setup

Before we do any work with Git, we have to set some settings:

  1. git config --global user.name "My Name"
  2. git config --global user.email email@email.com
  3. git config --global core.editor SOME_EDITOR
  4. git config --global push.default simple

Your First Git Repo

Let's create an empty Git repository at ~/git-repo/

  1. mkdir ~/git-repo
  2. cd ~/git-repo
  3. git init
  4. Profit!

Your First Git Commit

Let's add a readme file to our Git repo:

  1. YOUR_EDITOR README.md
  2. git add README.md
  3. git commit -m 'add readme file'

What Just Happened?

Let's take a minute to look at what we just did:

  1. Edit a file
  2. Add that file to the staging area
  3. Commit changes in the staging area to the repository

On the next slides, we will look at each step more in depth with the git status command:

What Just Happened?

Edit File

$ vi README.md
$ git status
On branch master

Initial commit

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

    README.md

nothing added to commit but untracked files present (use "git add" to track)

What Just Happened?

Add File To Staging Area

$ git add README.md
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   README.md

What Just Happened?

Commit Changes to Repository

$ git commit -m 'add readme file'
$ git status
On branch master
nothing to commit, working directory clean

What Just Happened?

Visual Overview

image

What Just Happened?

Check the Log

$ git log
commit c9fd3e8dc41e0649fc3ba0e88bcc2f41a97d8144
Author: rogerskw <rogerskw@miamioh.edu>
Date:   Wed Oct 22 15:00:53 2014 -0400

    asdf

If you have many commits, the less command will be used.

Let's Try Something Else

Remote Repositories

A remote is a copy of the repo in another location such as a server (like GitHub)

Save commits to a remote repository using

$ git push origin master

Where 'origin' is the remote's name and 'master' is the current branch's name

Let's Try Something Else

Branches

What many beginner git repos look like:

image

Let's Try Something Else

Branches

Taking full advantage of git branching

image

Branching

Create a new branch with

$ git branch newbranchname

Switch to the branch with

$ git checkout newbranchname

Make some changes and commit them

Branching

After committing, switch back to the 'master' branch

$ git checkout master

Look for your changes

What happened?

Branching

Merge your changes into the main development line

$ git merge newbranchname

Now you should see your changes from your branch

Branching

Large projects will have many branches

image

Other Cool Stuff

Stashing

You can use git stash in order to save changes to a branch

without actually committing

This can be useful if you made changes that aren't working,

but need to go to another branch to do something else

Other Cool Stuff

Tagging

Tag commits in order to give them an easier ID

Makes it easier to revert back to or identify releases

Other Cool Stuff

Blame

Shows who exactly committed each line of code

Useful for identifying who introduced a bug

Also useful for determining how much each person actually contributed

Other Cool Stuff

Reset

Can be used to reset the repo to a previous commit

Bisect

Do a binary search on commits in order to determine when a bug was introduced

Questions?