Let's Learn Git

MidCamp 2015

#midcamp

Alina Mackenzie

University of Illinois at Chicago

alimac

Storytelling

Photo by Suzanne Koch, Flickr.com

Version control

  • record changes
  • revert to an earlier state
  • tell a story

Photo by Annadriel, Flickr.com

Writing stories about our projects with git

Photo by Annadriel, Flickr.com

Distributed vs centralized

Photo by Susanne Nilsson, Flickr.com

Centralized

Diagram of a centralized version control system

Photo by Susanne Nilsson, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Distributed

Diagram of a distributed version control system

Photo by Susanne Nilsson, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Hosting

Photo by Susanne Nilsson, Flickr.com

Commands or app

Commandline

Mac, Windows, Linux, Solaris

Apps

  • SourceTree
  • GitHub for
  • SmartGit

Photo by Bill Gracey, Flickr.com

SourceTree

Screenshot of SourceTree app

Configure

Scope

  1. system
  2. user (global)
  3. directory (local)
  4. Photo by Daniel, Flickr.com

Name and email


git config --global user.name "Alina Mackenzie"
git config --global user.email hello@alim.ac
              

Photo by Daniel, Flickr.com

Show configuration


git config -l
  user.name=Alina Mackenzie
  user.email=alimknz@gmail.com
              

Photo by Daniel, Flickr.com

Try these

git config --global

color.ui true
              

core.excludesfile /Users/alimac/.gitignore
              

core.whitespace trailing-space
              

Photo by Daniel, Flickr.com

The setting

local repository
a project folder on my computer
remote repository
repository that multiple people contribute to

Photo by El Condor, Flickr.com

Packing list

  1. packing-list
  2. README.md
  3. Contents of README.md
    
    # Packing List
    
    Packing list for trips.
    

Photo by El Condor, Flickr.com

Create repository


cd packing-list
git init
  Initialized empty Git repository in /Users/alimac/packing-list/.git/
              
.git/
hidden folder where your repository's snapshots and history are stored

Photo by El Condor, Flickr.com

Phases

Untracked
Git sees it, but doesn't know about it.
Tracked
Git knows about it and tracks changes.
Staged
Changes are ready to be commited.
Committed
Changes to the file are saved to history.
Pushed
Changes are pushed to remote repository.

Photo by Luis Argerich, Flickr.com

Three sidekicks

"Where am I?"
"What is going on?"
"What did I just do?"

Photo by christina robinson, Flickr.com

Status

Where am I?

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)
              

Photo by christina robinson, Flickr.com

Status


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
              

Photo by christina robinson, Flickr.com

README.md


# Packing List

Packing list for trips.

* laptop
* skirt with pockets
* toothbrush
              

Photo by christina robinson, Flickr.com

Status


On branch master
Initial commit

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

  modified:   README.md

Changes not staged for commit:
  (use "git add <file>.." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   README.md
              

Photo by christina robinson, Flickr.com

Diff

What's going on?

git diff
  diff --git a/README.md b/README.md
  index daa8d60..da0a4b2 100644
  --- a/README.md
  +++ b/README.md
  @@ -1,3 +1,7 @@
   # Packing List

   Packing list for trips.
  +
  +* laptop
  +* skirt with pockets
  +* toothbrush
              

Photo by christina robinson, Flickr.com

Let's make history


git commit -m "Initial commit."
  [master (root-commit) 1c50eb4] Initial commit.
  1 file changed, 3 insertions(+)
  create mode 100644 README.md
              

Photo by christina robinson, Flickr.com

log

What just happened?

git log
  commit b9fd7596e44051a99b683a19982c592fdcbca5a5
  Author: Alina Mackenzie <hello@alim.ac>
  Date:   Wed Mar 8 12:10:25 2015 -0500

    Initial commit.
              

Photo by christina robinson, Flickr.com

Commit

"A set of related and reversible changes that function together as a unit of work."

Photo by Kathleen Typer-Conklin, Flickr.com

Interactive staging

git add -p
Asks which changes to stage

README.md


# Packing List

Packing list for trips.

## Technology
* laptop

## Clothes
* skirt with pockets

## Toiletries
* toothbrush
              

Photo by christina robinson, Flickr.com

git add -p


diff --git a/README.md b/README.md
index da0a4b2..9e775a7 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,11 @@

 Packing list for trips.

+## Technology
 * laptop
+
+## Clothes
 * skirt with pockets
+
+## Toiletries
 * toothbrush
Stage this hunk [y,n,q,a,d,/,s,e,?]?
              

Photo by christina robinson, Flickr.com

Split


Stage this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 3 hunks.
@@ -2,4 +2,5 @@

 Packing list for trips.

+## Technology
 * laptop
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?
              

Photo by christina robinson, Flickr.com

Commit message

git commit

Short descriptive subject line.

Longer description in the form of paragraphs
that include reasons for the change, reference
tickets or other commits.
              

Photo by christina robinson, Flickr.com

Push

Add a remote repository


git add remote origin git@bitbucket.org:alimac/packing-list.git
              

Push commits to remote repository


git push -u origin master
              

Photo by christina robinson, Flickr.com

Undo

Not committed yet?

unstage a file
git reset HEAD README.md
git reset -p
undo all changes to a file
git checkout -- README.md

Photo by Melissa Hincha-Ownby, Flickr.com

Committed but not pushed?

update last commit
stage or unstage files as you normally would
git commit --amend
undo last commit
git reset --soft HEAD~1
git reset HEAD~1 (unstage, too)

Photo by Melissa Hincha-Ownby, Flickr.com

More than one commit?

undo last 3 commits
git reset --soft HEAD~3
git reset HEAD~3
undo a specific commit
git revert 617ff153

Photo by Melissa Hincha-Ownby, Flickr.com

Pushed?

Don't do it.

But if you have to...

... and you are the only contributor

git push --force <remote> <branch>

Photo by Melissa Hincha-Ownby, Flickr.com

Branch

  • cheap way to create copies of the project
  • multiple copies in one directory
  • easy to merge

Photo by lamoix, Flickr.com

Create branch

git branch testing Diagram showing master and testing branch, HEAD pointing to master.

Photo by lamoix, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Switch to branch

git checkout testing Diagram showing HEAD pointing to testing branch.

Photo by lamoix, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Make commits to testing branch

Diagram showing testing branch advancing past master.

Photo by lamoix, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Switch back to master branch

git checkout master Diagram showing HEAD pointing back to master.

Photo by lamoix, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Merge

merge testing into master


git checkout master
git merge testing
              

Photo by Nige Harris, Flickr.com

Fast-forward merge

Replay commits of one branch onto another

Diagram showing a branch that can be merged by fast-forward.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Recursive merge

Branches have diverged

Diagram showing diverged branches.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Merge commit

Tie two branch endpoints

Diagram showing merge commit.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Rebase

  • merge without a merge commit
  • rewrites history
  • changes commit hashes

Photo by Leo Reynolds, Flickr.com

Divergent branches

Diagram showing divergent branches.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Rebase


git checkout experiment
git rebase master
              
Diagram showing a basic rebase.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Fast-forward merge


git checkout master
git merge experiment
              
Diagram showing branch after rebase.

Photo by Nige Harris, Flickr.com | Diagram by Scott Chacon and Ben Straub, git-scm.com

Conflict

File has been modified in the same place:

  1. stops activity (merge, rebase)
  2. lists files with conflicts
  3. resolve conflicts
  4. add (stage) files to continue activity

Photo by Holger, Flickr.com

Conflict markers


Question: How many planets are there in the solar system?
<<<<<<< HEAD
Answer: eight.
=======
Answer: nine.
>>>>>>> branch-a
              

Replace text between markers with your resolution and delete markers.

Photo by Holger, Flickr.com

Resolve


Question: How many planets are there in the solar system?
Answer: eight.
              

git add <file> to mark conflict resolved and continue merge or rebase

Photo by Holger, Flickr.com

Resources

Photo by Moyann Brenn, Flickr.com

Sprint Sunday 09:00-17:00

UIC SCE Tower, Room 605 750 S. Halsted

http://2015.midcamp.org/schedule

Learn through contribution

Contributors of all skill sets and levels are welcome and encouraged.

Feedback

http://bit.ly/midcamp-git

Alina Mackenzie: alimac

#MidCamp