github - git training slides: foundations

537
Git Foundations An exploration of the Git toolbox Official training curriculum v3.1.0 © 2012, GitHub, Inc.

Upload: lee-hanxue

Post on 06-May-2015

1.111 views

Category:

Technology


6 download

DESCRIPTION

Slide deck with detailed step breakdown that explains how git works, together with simple examples that you can try out yourself. Slides originated from http://teach.github.com/articles/course-slides/ Author: https://twitter.com/matthewmccull

TRANSCRIPT

Page 1: Github - Git Training Slides: Foundations

Git FoundationsAn exploration of the Git toolbox

Offi

cial

trai

ning

cur

ricul

um v

3.1.

201

2, G

itHub

, Inc

.

Page 2: Github - Git Training Slides: Foundations

Hello!

Page 3: Github - Git Training Slides: Foundations

Matthew McCullough

@matthewmccull

Page 4: Github - Git Training Slides: Foundations

Ma!hew who?‣Open source contributor‣Build tool book co-author‣Continuous integration book co-author‣5 year Git evangelist‣VP of Training at GitHub

Page 5: Github - Git Training Slides: Foundations

Welcome

‣Questions, Pacing‣Ask questions at any time. Don't wait!‣Suggest course pacing slowing/speeding up

Page 6: Github - Git Training Slides: Foundations
Page 7: Github - Git Training Slides: Foundations
Page 8: Github - Git Training Slides: Foundations
Page 9: Github - Git Training Slides: Foundations

GitWhere’s this coming from?

Page 10: Github - Git Training Slides: Foundations

Git

Page 11: Github - Git Training Slides: Foundations

Git?

Open Source

Page 12: Github - Git Training Slides: Foundations

Git?

bash scripts � C code

Page 13: Github - Git Training Slides: Foundations

Git?

Page 14: Github - Git Training Slides: Foundations

Git-nounBritish Slang. an unpleasant

or contemptible person

-Oxford English Dictionary

Page 16: Github - Git Training Slides: Foundations

GitWhat is this thing?

Page 17: Github - Git Training Slides: Foundations

GIT - the stupid content tracker

"git" can mean anything, depending on

your mood.

* random three-letter combination that

is pronounceable, and not actually

used by any

common UNIX command. The fact that it

is a mispronunciation of "get" may or

may not be

relevant.* stupid. contempti

ble and despicable. simple. Take your

pick from the dictionary of

slang.* "global informati

on tracker": you're in a good mood, an

d it actually works for you.

Angels sing, and a light suddenly fill

s the room.

* "goddamn idiotic truckload of sh*t":

when it breaks

Git is a fast, scalable, distributed r

evision control system with an unusual

ly rich

command set that provides both high-le

vel operations and full access to inte

rnals.

Git is an Open Source project covered

by the GNU General Public License. It

was originally

written by Linus Torvalds with help of

a group of hackers around the net. It

is currently

maintained by Junio C Hamano.

Page 18: Github - Git Training Slides: Foundations

file trackercontent

Page 19: Github - Git Training Slides: Foundations

Git?

centralized version control systems have matured

Page 20: Github - Git Training Slides: Foundations

CVS

Subversion

PVCS

Perforce

ClearCaseSource

Safe

RCS

Folders

Git?

Page 21: Github - Git Training Slides: Foundations

small improvements, but noradical innovation

Git?

Page 22: Github - Git Training Slides: Foundations

Linus?

Git?

Page 23: Github - Git Training Slides: Foundations

I did end up using CVS for 7 years at a commercial

company and I hate it with a passion...

The slogan of Subversion for a while was

"CVS done right"... and if you start with that

kind of slogan, there's nowhere you can go.

There is no way to do CVS right.-Linus Torvalds

Page 24: Github - Git Training Slides: Foundations

VCS reboot

Git?

Page 25: Github - Git Training Slides: Foundations

50% Distributed Version Control

Git?

50% Git Concepts

with

Page 26: Github - Git Training Slides: Foundations

1997code co-op

Page 27: Github - Git Training Slides: Foundations

2001arch

Page 28: Github - Git Training Slides: Foundations

2003monotone

Page 29: Github - Git Training Slides: Foundations

2003SVK

Page 30: Github - Git Training Slides: Foundations

2003darcs

Page 31: Github - Git Training Slides: Foundations

2005bazaar

Page 32: Github - Git Training Slides: Foundations

2005mercurial

Page 33: Github - Git Training Slides: Foundations

2005git

Page 34: Github - Git Training Slides: Foundations

time to mature

Page 35: Github - Git Training Slides: Foundations

most unique improvements

Page 36: Github - Git Training Slides: Foundations

largest DVCS user base

Page 37: Github - Git Training Slides: Foundations

SetupTesting Git

Page 38: Github - Git Training Slides: Foundations

Setting up Git

Check your Git version... git --version

Page 39: Github - Git Training Slides: Foundations

SetupWhat is a Git install?

Page 40: Github - Git Training Slides: Foundations

Setting up Git

binaries on your $PATH

Page 41: Github - Git Training Slides: Foundations

UsingCreating a repository

Page 42: Github - Git Training Slides: Foundations

Creating A Repository

# Green field project$ git init newproject$ cd newproject# ...start coding

Page 43: Github - Git Training Slides: Foundations

Creating A Repository

‣Create our first repository

Page 44: Github - Git Training Slides: Foundations

Creating A Repository

or if you already have source code

Page 45: Github - Git Training Slides: Foundations

Creating A Repository

# Legacy project tree$ cd existingproject$ git init

# Add all the code$ git add .$ git commit -m”Initial import”

Page 46: Github - Git Training Slides: Foundations

UsingWhat’s in .git?

Page 47: Github - Git Training Slides: Foundations

Contents of .git

.git!"" COMMIT_EDITMSG!"" HEAD!"" MERGE_RR!"" config!"" description!"" hooks#   !"" pre-commit.sample#   $"" update.sample!"" index!"" info#   $"" exclude!"" logs#   !"" HEAD#   $"" refs#   $"" heads#   $"" master!"" objects#   !"" 54#   #   $"" 3b9bebdc6bd5c4b22136034a95dd097a57d3dd#   !"" info#   $"" pack!"" refs    !"" heads    #   $"" master    $"" tags

Page 48: Github - Git Training Slides: Foundations

Three stage thinking

‣Explore the .git folder

Page 49: Github - Git Training Slides: Foundations

ConfigurationDisplay

Page 50: Github - Git Training Slides: Foundations

Configuring Git

Query existing configuration

Page 51: Github - Git Training Slides: Foundations

Configuring Git

All entries

Page 52: Github - Git Training Slides: Foundations

Configuring Git

#List all config valuesgit config --list

Page 53: Github - Git Training Slides: Foundations

Configuring Git

Single entry

Page 54: Github - Git Training Slides: Foundations

Configuring Git

#Query effective value of a single keygit config section.keygit config section.subsection.key

Page 55: Github - Git Training Slides: Foundations

Configuring Git

#Show a specific config valuegit config user.namegit config user.email

Page 56: Github - Git Training Slides: Foundations

ConfigurationLayers

Page 57: Github - Git Training Slides: Foundations

Config Targets

git config --system#Saves to /etc/gitconfig

Page 58: Github - Git Training Slides: Foundations

Config Targets

git config --global#Saves to ~/.gitconfig

Page 59: Github - Git Training Slides: Foundations

Config Targets

git config --local#Saves to .git/config

Page 60: Github - Git Training Slides: Foundations

Configuring Git

#Configure a setting in a .git repository

git config __________git config --local __________

#Configure a setting in the user's home dir

git config --global __________

#Configure a setting in the Git install dir# e.g. /usr/local/Cellar/git/1.7.x/etc/gitconfig

git config --system __________

Page 61: Github - Git Training Slides: Foundations

Config Targets

Git configuration reading & writing

targets local by default

Page 62: Github - Git Training Slides: Foundations

ConfigurationInheritance

Page 63: Github - Git Training Slides: Foundations

Config Inheritance

Query existing configuration by layer

Page 64: Github - Git Training Slides: Foundations

Config Inheritance

#Query a single key in a single layergit config --<WHERE> section.keygit config --<WHERE> section.subsection.key

Page 65: Github - Git Training Slides: Foundations

Config Inheritance

#List all system config valuesgit config --system --list

Page 66: Github - Git Training Slides: Foundations

Config Inheritance

#List all global config valuesgit config --global --list

Page 67: Github - Git Training Slides: Foundations

Config Inheritance

#List all local config valuesgit config --local --list

Page 68: Github - Git Training Slides: Foundations

Config Inheritance

#List effective config valuesgit config --list

Page 69: Github - Git Training Slides: Foundations

ConfigurationSet user identity

Page 70: Github - Git Training Slides: Foundations

Set User Identity

any config at any layer

Page 71: Github - Git Training Slides: Foundations

Set User Identity

#List the current config git config --global user.name "Fird Birfle"git config --global user.email "[email protected]"

Page 72: Github - Git Training Slides: Foundations

Set User Identity

just a string

Page 73: Github - Git Training Slides: Foundations

Set User Identity

user identity

Page 74: Github - Git Training Slides: Foundations

Set User Identity

user identityauthenticationnot

Page 75: Github - Git Training Slides: Foundations

Set User Identity

user identityauthenticationauthorization not

Page 76: Github - Git Training Slides: Foundations

ConfigurationDisplay color

Page 77: Github - Git Training Slides: Foundations

Set Console Color

console color

Page 78: Github - Git Training Slides: Foundations

Set Console Color

git config --global color.ui always

Page 79: Github - Git Training Slides: Foundations

Set Console Color

git log \ --graph \ --decorate \ --simplify-by-decoration \ --abbrev-commit \ --date=relative \ --pretty=oneline \ --all

Page 80: Github - Git Training Slides: Foundations
Page 81: Github - Git Training Slides: Foundations

Configuring Git

git log \ --graph \ --decorate \ --simplify-by-decoration \ --abbrev-commit \ --date=relative \ --pretty=oneline \ --all \ | more

Page 82: Github - Git Training Slides: Foundations
Page 83: Github - Git Training Slides: Foundations

Set Console Color

Bleh!

Page 84: Github - Git Training Slides: Foundations

Set Console Color

output destination detection

Page 85: Github - Git Training Slides: Foundations

Set Console Color

git config --global color.ui auto

# or the identical effect with...git config --global color.ui true

Page 86: Github - Git Training Slides: Foundations

Set Console Color

git log \ --graph \ --decorate \ --simplify-by-decoration \ --abbrev-commit \ --date=relative \ --pretty=oneline \ --all \ | more

Page 87: Github - Git Training Slides: Foundations
Page 88: Github - Git Training Slides: Foundations

Set Console Color

when you need itColor

Page 89: Github - Git Training Slides: Foundations

Set Console Color

Set Console Color

only when you need itColor

Page 90: Github - Git Training Slides: Foundations

ConfigurationLine endings

Page 91: Github - Git Training Slides: Foundations

Line Endings

line endings

Page 92: Github - Git Training Slides: Foundations

Line Endings

VS

Page 93: Github - Git Training Slides: Foundations

Line Endings

VSLF

CRLF

Page 94: Github - Git Training Slides: Foundations

Line Endings

default is to do nothing

Page 95: Github - Git Training Slides: Foundations

Line Endings

http://help.github.com/dealing-with-lineendings/

Page 96: Github - Git Training Slides: Foundations

Line Endings

#Force files to be LF in the repo,# even on Mac/Linuxgit config --global core.autocrlf input

#Force Windows to convert to platform# on checkout and to LF on commitgit config --global core.autocrlf true

Page 97: Github - Git Training Slides: Foundations

Line Endings

#Force files to be LF during `add`git config --global core.autocrlf input

#Force Windows to convert to CRLF# on checkout and to LF on `add`git config --global core.autocrlf true

Page 98: Github - Git Training Slides: Foundations

warn about conversion

Page 99: Github - Git Training Slides: Foundations

Line Endings

#Never complain about line ending conversiongit config --global core.safecrlf false

#Warn, but allow line ending conversion to proceed#(the default)git config --global core.safecrlf warn

#Do not allow line ending conversion to proceedgit config --global core.safecrlf true

Page 100: Github - Git Training Slides: Foundations

ConfigurationSecure Sockets Host (SSH)

Page 101: Github - Git Training Slides: Foundations

Configuring SSH

‣Generate an ssh key pair

Page 102: Github - Git Training Slides: Foundations

Configuring SSH

ssh key pair

Page 103: Github - Git Training Slides: Foundations

Configuring SSH

$ ssh-keygen -t rsa -C”For GitHub”

Page 104: Github - Git Training Slides: Foundations

Configuring SSH

$ ssh-keygen -t rsa -C”For GitHub”

Page 105: Github - Git Training Slides: Foundations

Configuring SSH

Page 106: Github - Git Training Slides: Foundations

Configuring SSH

# Verify the files were created

$ cd ~/.ssh$ ls

Page 107: Github - Git Training Slides: Foundations

Configuring SSH

Page 108: Github - Git Training Slides: Foundations

Configuring SSH

‣id_rsa is the private half of the key‣Keep this uncompromisingly secret

‣id_rsa.pub is the public half of the key‣Give this away freely

Page 109: Github - Git Training Slides: Foundations

Configuring SSH

#GitHub sanity testssh -T [email protected]

> Hi matthewmccullough! You've successfully authenticated, but GitHub does not provide shell access.

Page 110: Github - Git Training Slides: Foundations

Configuring SSH

#GitHub sanity test with verbose SSHssh -v [email protected]

>OpenSSH_5.2p1, OpenSSL 0.9.8l 5 Nov 2009debug1: Reading configuration data /Users/mccm06/.ssh/configdebug1: Reading configuration data /etc/ssh_configdebug1: Connecting to github.com [207.97.227.239] port 22.debug1: Connection established.debug1: identity file /Users/mccm06/.ssh/identity type -1debug1: identity file /Users/mccm06/.ssh/id_rsa type 1debug1: identity file /Users/mccm06/.ssh/id_dsa type 2...debug1: Host 'github.com' is known and matches the RSA host key.debug1: Found key in /Users/mccm06/.ssh/known_hosts:3debug1: ssh_rsa_verify: signature correct...debug1: Trying private key: /Users/mccm06/.ssh/identitydebug1: Offering public key: /Users/mccm06/.ssh/id_rsadebug1: Remote: Forced command: gerve matthewmccullough

Page 111: Github - Git Training Slides: Foundations

Using GitThree Stage Thinking

Page 112: Github - Git Training Slides: Foundations

Three stage thinking

‣Edit‣Add‣Commit

Page 113: Github - Git Training Slides: Foundations

Three stage thinking

Working Staging Repo

add

commit

edit

Page 114: Github - Git Training Slides: Foundations

Three stage thinking

Page 115: Github - Git Training Slides: Foundations

‣shopping cart‣put things in‣take things out‣purchase at register

Page 116: Github - Git Training Slides: Foundations

Three stage thinking

Page 117: Github - Git Training Slides: Foundations

Three stage thinking

‣database transaction‣update values‣insert rows‣delete rows‣commit transaction

Page 118: Github - Git Training Slides: Foundations

Three stage thinking

only one staging area

Page 119: Github - Git Training Slides: Foundations

Three stage thinking

Working Staging Repo

add

commit

edit

Page 120: Github - Git Training Slides: Foundations

SetupCommit Message Editor

Page 121: Github - Git Training Slides: Foundations

Commit Message Editor

Page 122: Github - Git Training Slides: Foundations

✓? Used the VI editor?

Page 123: Github - Git Training Slides: Foundations

Commit Message Editor

Iy;WQ

Page 124: Github - Git Training Slides: Foundations

Commit Message Editor

an alternate editor?

Page 125: Github - Git Training Slides: Foundations

Commit Message Editor

$EDITOR environment variable

Option 1

Page 126: Github - Git Training Slides: Foundations

Commit Message Editor

$ export EDITOR=<AnEditorOnYourPath>

Option 1

Page 127: Github - Git Training Slides: Foundations

Commit Message Editor

GitPad wraps Notepad (or $EDITOR)

Option 2

Page 128: Github - Git Training Slides: Foundations

Commit Message Editor

http://github.com/github/GitPad

Option 2

Page 129: Github - Git Training Slides: Foundations

Commit Message Editor

Run GitPad...registers & wraps Notepad as the editor

Option 2

Page 130: Github - Git Training Slides: Foundations

Commit Message Editor

Git-specific configuration option

Option 3

Page 131: Github - Git Training Slides: Foundations

Commit Message Editor

#for TextMate on Macgit config --global core.editor "mate -w"

#for Notepad2 on Windowsgit config --global core.editor "notepad2.exe"

#for emacs on Linuxgit config --global core.editor "emacs"

Option 3

Page 132: Github - Git Training Slides: Foundations

Commit Message Editor

Option 3

Page 133: Github - Git Training Slides: Foundations

Commit Message Editor

‣Toggle to alternate editor

‣Make a commit

‣Test that the new editor pops up

Page 134: Github - Git Training Slides: Foundations

Usage BasicsAdding & committing code

Page 135: Github - Git Training Slides: Foundations

Three stage thinking

$ vi first.html

$ git status$ git add first.html$ git commit -m”First commit”

Page 136: Github - Git Training Slides: Foundations

Three stage thinking

‣Write a sample HTML or TXT file

‣Inspect Git’s status

‣Add code (stage it)

‣Commit code

Page 137: Github - Git Training Slides: Foundations

Usage BasicsDiff-ing changes

Page 138: Github - Git Training Slides: Foundations

Three stage thinking

What has changed that we haven’t committed?

Page 139: Github - Git Training Slides: Foundations

Three stage thinking

# Show the unstaged changes$ git diff

Page 140: Github - Git Training Slides: Foundations

Three stage thinking

Working Staging Repo

$ git diff

Page 141: Github - Git Training Slides: Foundations

Three stage thinking

# Show the staged changes$ git diff --staged

Page 142: Github - Git Training Slides: Foundations

Three stage thinking

Working Staging Repo

$ git diff --staged

Page 143: Github - Git Training Slides: Foundations

Three stage thinking

# Show uncommitted changes$ git diff HEAD

Page 144: Github - Git Training Slides: Foundations

Three stage thinking

Working Staging Repo

$ git diff HEAD

Page 145: Github - Git Training Slides: Foundations

Usage BasicsLimiting diff output

Page 146: Github - Git Training Slides: Foundations

Limiting Diff Output

Word changes instead of entire lines?

Page 147: Github - Git Training Slides: Foundations

Limiting Diff Output

git diff --color-words

Page 148: Github - Git Training Slides: Foundations

Limiting Diff Output

$ git diffdiff --git a/first.txt b/first.txtindex cbb1543..e99dea9 100644--- a/first.txt+++ b/first.txt@@ -1,4 +1,4 @@-//Round the rugged rock+//Round the ragged rock

$ git diff --color-wordsdiff --git a/first.txt b/first.txtindex cbb1543..e99dea9 100644--- a/first.txt+++ b/first.txt@@ -1,4 +1,4 @@//Round the ruggedragged rock

Page 149: Github - Git Training Slides: Foundations

Limiting Diff Output

git diff --word-diff

Page 150: Github - Git Training Slides: Foundations

Limiting Diff Output

$ git diffdiff --git a/first.txt b/first.txtindex cbb1543..e99dea9 100644--- a/first.txt+++ b/first.txt@@ -1,4 +1,4 @@-//Round the rugged rock+//Round the ragged rock

$ git diff --word-diffdiff --git a/first.txt b/first.txtindex cbb1543..e99dea9 100644--- a/first.txt+++ b/first.txt@@ -1,4 +1,4 @@//Round the [-rugged-]{+ragged+} rock

Page 151: Github - Git Training Slides: Foundations

Usage BasicsWhitespace diff

Page 152: Github - Git Training Slides: Foundations

Limiting Diff Output

whitespace suppressed?

Page 153: Github - Git Training Slides: Foundations

Limiting Diff Output

refactoring review

Page 154: Github - Git Training Slides: Foundations

Limiting Diff Output

git diff -w

Page 155: Github - Git Training Slides: Foundations

Limiting Diff Output

$ git diffdiff --git a/first.txt b/first.txtindex 09195c0..f2c3243 100644--- a/first.txt+++ b/first.txt@@ -1,4 +1,4 @@-//Foo-//Bar-//Baz+ //Foo+// Bar+//Baz▊

$ git diff -w$

Page 156: Github - Git Training Slides: Foundations

Usage BasicsLimiting diff by type

Page 157: Github - Git Training Slides: Foundations

Limiting Diff Output

# Added (A)# Copied (C)# Deleted (D)# Modified (M)# Renamed (R)# Type changed (T)# Unmerged (U)# Unknown (X)# Pairing Broken (B)

# Only show changes in added filesgit diff --diff-filter=A

Page 158: Github - Git Training Slides: Foundations

Limiting Diff Output

# Added (A)# Copied (C)# Deleted (D)# Modified (M)# Renamed (R)# Type changed (T)# Unmerged (U)# Unknown (X)# Pairing Broken (B)

# Only show changes in modified filesgit diff --diff-filter=M

Page 159: Github - Git Training Slides: Foundations

Limiting Diff Output

# Added (A)# Copied (C)# Deleted (D)# Modified (M)# Renamed (R)# Type changed (T)# Unmerged (U)# Unknown (X)# Pairing Broken (B)

# Only show changes in added or modified filesgit diff --diff-filter=AM

Page 160: Github - Git Training Slides: Foundations

Limiting Diff Output

$ git status -s -u

D READMEMM first.txt

$ git diff --diff-filter=M

diff --git a/first.txt b/first.txtindex 71b55ef..14e4853 100644--- a/first.txt+++ b/first.txt@@ -1,3 +1,3 @@-//Foo //Bar+//Baz

Only first.txt is reported

Page 161: Github - Git Training Slides: Foundations

Usage BasicsReviewing history

Page 162: Github - Git Training Slides: Foundations

Three stage thinking

view history of commits

Page 163: Github - Git Training Slides: Foundations

Three stage thinking

# Show all historygit log

Page 164: Github - Git Training Slides: Foundations

Three stage thinking

# Show all history with filenamesgit log --stat

Page 165: Github - Git Training Slides: Foundations

Three stage thinking

# Show all history with patchesgit log --patchgit log -p

Page 166: Github - Git Training Slides: Foundations

Three stage thinking

# Limit the output entriesgit log -1git log -3git log -5

Page 167: Github - Git Training Slides: Foundations

Three stage thinking

# Control the output formatgit log --pretty=full

Page 168: Github - Git Training Slides: Foundations

Three stage thinking

# Control the output formatgit log --pretty=fuller

Page 169: Github - Git Training Slides: Foundations

Three stage thinking

# Control the output formatgit log --pretty=email

Page 170: Github - Git Training Slides: Foundations

Three stage thinking

# Control the output formatgit log --pretty=raw

Page 171: Github - Git Training Slides: Foundations

Three stage thinking

# Control the output formatgit log --pretty=format:<pattern>

Page 172: Github - Git Training Slides: Foundations

Three stage thinking

# Limit the output to added filesgit log --diff-filter=A

Page 173: Github - Git Training Slides: Foundations

Usage Basics+Ignoring files

Page 174: Github - Git Training Slides: Foundations

Ignoring Files

Untracked TrackedUnmodified

TrackedModified

TrackedStaged

Ignored

commit

add

edit

add

rm

ignore

Page 175: Github - Git Training Slides: Foundations

Ignoring Files

suppressing files from being reported as untracked

Page 176: Github - Git Training Slides: Foundations

Ignoring Files

glob patterns

Page 177: Github - Git Training Slides: Foundations

Ignoring Files

$ vim .gitignore

#Add glob patterns, one per line*.log*.tmp

Page 178: Github - Git Training Slides: Foundations

Ignoring Files

in-memory recursive evaluation

Page 179: Github - Git Training Slides: Foundations

Ignoring Files

$ vim .gitignore

#Add glob patterns, one per line*.log*.tmptargetoutput/!special.log

Page 180: Github - Git Training Slides: Foundations

Ignoring Files

‣ Ignore files via local .gitignore

Page 181: Github - Git Training Slides: Foundations

Usage Basics+Open source .gitignores

Page 182: Github - Git Training Slides: Foundations

Open Source Ignores

Preconfigured .gitignore files

Page 183: Github - Git Training Slides: Foundations

Open Source Ignores

http://github.com/github/gitignore

Page 184: Github - Git Training Slides: Foundations

copy and paste(the one time it’s OK to do it)

Page 185: Github - Git Training Slides: Foundations

Usage Basics+Global ignores

Page 186: Github - Git Training Slides: Foundations

Global Ignore

Global .gitignore ?

Page 187: Github - Git Training Slides: Foundations

Global Ignore

off by default

Page 188: Github - Git Training Slides: Foundations

Global Ignore

$ git config --global core.excludesfile "~/.gitignore"

Page 189: Github - Git Training Slides: Foundations

Global Ignore

$ vim ~/.gitignore

# Operating system and editor temp files# Generally redundant over project .gitignoresthumbs.db.DS_Store

Page 190: Github - Git Training Slides: Foundations

Global Ignore

‣ Ignore files via global ~/.gitignore

Page 191: Github - Git Training Slides: Foundations

Usage Basics+Removing files

Page 192: Github - Git Training Slides: Foundations

The Git File Workflow

Untracked TrackedUnmodified

TrackedModified

TrackedStaged

Ignored

commit

add

edit

add

rm

ignore

Page 193: Github - Git Training Slides: Foundations

The Git File Workflow

Removing Files

Page 194: Github - Git Training Slides: Foundations

The Git File Workflow

# Directly remove & stage$ git rm <FILENAME>

Page 195: Github - Git Training Slides: Foundations

The Git File Workflow

# Remove with OS or tool,# not integrated with Git$ rm <FILENAME>

# Staging area says it is# deleted but not staged$ git status

# Put deletion into staging$ git rm <FILENAME>

Page 196: Github - Git Training Slides: Foundations

The Git File Workflow

# Remove with OS or tool# then follow up with git add$ rm <FILENAMES>$ git add -u .

Page 197: Github - Git Training Slides: Foundations

The Git File Workflow

‣ Remove files

Page 198: Github - Git Training Slides: Foundations

Usage Basics+Moving files

Page 199: Github - Git Training Slides: Foundations

The Git File Workflow

Untracked TrackedUnmodified

TrackedModified

TrackedStaged

Ignored

commit

add

edit

add

rm

ignore

Page 200: Github - Git Training Slides: Foundations

The Git File Workflow

Moving Files

Page 201: Github - Git Training Slides: Foundations

The Git File Workflow

# Directly move & stage$ git mv <FILENAME> <NEWFILENAME>

Page 202: Github - Git Training Slides: Foundations

The Git File Workflow

# Move with OS or tool,mv <FILENAME> <NEWFILENAME># Then follow up with git addgit add -A .

Page 203: Github - Git Training Slides: Foundations

The Git File Workflow

‣ Rename (move) files

‣ View history of the move

Page 204: Github - Git Training Slides: Foundations

Usage Basics+Similarity index

Page 205: Github - Git Training Slides: Foundations

Similarity Index

no "move" primitive

Page 206: Github - Git Training Slides: Foundations
Page 207: Github - Git Training Slides: Foundations
Page 208: Github - Git Training Slides: Foundations

Similarity Index

“similarity index”

Page 209: Github - Git Training Slides: Foundations
Page 210: Github - Git Training Slides: Foundations

Similarity Index

score of sameness

Page 211: Github - Git Training Slides: Foundations

Usage Basics+Similarity index for moves

Page 212: Github - Git Training Slides: Foundations

Similarity Index

# Move with OS or tool,$ mv <FILENAME> <NEWFILENAME>

# Follow up by staging everything$ git add -A .

# Renames showinggit status

# No renames showing?git log --stat

Page 213: Github - Git Training Slides: Foundations

why no renames in history?

Page 214: Github - Git Training Slides: Foundations

Similarity Index

# Renames showngit log --stat -M

Page 215: Github - Git Training Slides: Foundations

Similarity Index

‣ Rename (move) files with changes

‣ Essentially, a refactoring workflow

Page 216: Github - Git Training Slides: Foundations

Usage Basics+Similarity index for copies

Page 217: Github - Git Training Slides: Foundations

Similarity Index

# Copies & renames shown# (superset of -M)git log --stat -C

Page 218: Github - Git Training Slides: Foundations

Similarity Index

‣Copy a file to a new filename

‣ Add and commit it

‣ Log it using our -C option

Page 219: Github - Git Training Slides: Foundations

“For performance reasons, by

default, -C option finds copies only if the original file

of the copy was modified in

the same changeset. ”

Page 220: Github - Git Training Slides: Foundations

Similarity Index

‣Modify a file and copy it (in the same commit)

‣ Log using the -C option

Page 221: Github - Git Training Slides: Foundations

“ The --find-copies-harder flag makes the

command inspect unmodified files as

candidates for the source of copy. This

is a very expensive operation for large

projects, so use it with caution. Giving

more than one -C option has the same

effect.”

Page 222: Github - Git Training Slides: Foundations

Similarity Index

# Copies & renames shown# (superset of -M)git log --stat -C -Cgit log --stat --find-copies-harder

Page 223: Github - Git Training Slides: Foundations

Similarity Index

‣ Log the same file using our -C -C option

Page 224: Github - Git Training Slides: Foundations

Usage Basics+Similarity index for blame

Page 225: Github - Git Training Slides: Foundations

Similarity Index

# File authoring showngit blame rerere.c

Page 226: Github - Git Training Slides: Foundations

Similarity Index

# True source of code showngit blame rerere.c -C

Page 227: Github - Git Training Slides: Foundations

Similarity Index

5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 1) #include "cache.h"c455c87c (Johannes Schindelin 2008-07-21 19:03:49 +0100 2) #include "string-list.h"5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 3) #include "rerere.h"5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 4) #include "xdiff-interface.h"dea4562b (Junio C Hamano 2009-12-25 15:51:32 -0800 5) #include "dir.h"dea4562b (Junio C Hamano 2009-12-25 15:51:32 -0800 6) #include "resolve-undo.h"dea4562b (Junio C Hamano 2009-12-25 15:51:32 -0800 7) #include "ll-merge.h"8588567c (Junio C Hamano 2010-01-16 23:28:46 -0800 8) #include "attr.h"5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 9) ac49f5ca (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 10) #define RESOLVED 0ac49f5ca (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 11) #define PUNTED 1ac49f5ca (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 12) #define THREE_STAGED 2ac49f5ca (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 13) void *RERERE_RESOLVED = &RERERE_RESOLVED;ac49f5ca (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 14) 5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 15) /* if rerere_enabled == -1, fall back to detection of .git5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 16) static int rerere_enabled = -1;5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 17) 5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 18) /* automatically update cleanly resolved paths to the inde5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 19) static int rerere_autoupdate;5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 20) 5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 21) static char *merge_rr_path;5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 22) 90056966 (SZEDER Gábor 2009-02-14 23:21:04 +0100 23) const char *rerere_path(const char *hex, const char *file)5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 24) {90056966 (SZEDER Gábor 2009-02-14 23:21:04 +0100 25) return git_path("rr-cache/%s/%s", hex, file);5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 26) }5b2fd956 (Stephan Beyer 2008-07-09 14:58:57 +0200 27)

Page 228: Github - Git Training Slides: Foundations

Similarity Index

5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 1) #include "cache.h"c455c87c rerere.c (Johannes Schindelin 2008-07-21 19:03:49 +0100 2) #include "string-list.h"5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 3) #include "rerere.h"5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 4) #include "xdiff-interface.h"dea4562b rerere.c (Junio C Hamano 2009-12-25 15:51:32 -0800 5) #include "dir.h"dea4562b rerere.c (Junio C Hamano 2009-12-25 15:51:32 -0800 6) #include "resolve-undo.h"dea4562b rerere.c (Junio C Hamano 2009-12-25 15:51:32 -0800 7) #include "ll-merge.h"8588567c rerere.c (Junio C Hamano 2010-01-16 23:28:46 -0800 8) #include "attr.h"5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 9) ac49f5ca rerere.c (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 10) #define RESOLVED 0ac49f5ca rerere.c (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 11) #define PUNTED 1ac49f5ca rerere.c (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 12) #define THREE_STAGED 2ac49f5ca rerere.c (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 13) void *RERERE_RESOLVED = &RERERE_RESOLVED;ac49f5ca rerere.c (Martin von Zweigbergk 2011-02-16 05:47:44 -0500 14) b4372ef1 builtin-rerere.c (Johannes Schindelin 2007-07-06 13:05:59 +0100 15) /* if rerere_enabled == -1, fall back to detection of .gitb4372ef1 builtin-rerere.c (Johannes Schindelin 2007-07-06 13:05:59 +0100 16) static int rerere_enabled = -1;b4372ef1 builtin-rerere.c (Johannes Schindelin 2007-07-06 13:05:59 +0100 17) 121c813f builtin-rerere.c (Junio C Hamano 2008-06-22 02:04:31 -0700 18) /* automatically update cleanly resolved paths to the inde121c813f builtin-rerere.c (Junio C Hamano 2008-06-22 02:04:31 -0700 19) static int rerere_autoupdate;121c813f builtin-rerere.c (Junio C Hamano 2008-06-22 02:04:31 -0700 20) 658f3650 builtin-rerere.c (Johannes Schindelin 2006-12-20 17:39:41 +0100 21) static char *merge_rr_path;658f3650 builtin-rerere.c (Johannes Schindelin 2006-12-20 17:39:41 +0100 22) 90056966 rerere.c (SZEDER Gábor 2009-02-14 23:21:04 +0100 23) const char *rerere_path(const char *hex, const char *file)5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 24) {90056966 rerere.c (SZEDER Gábor 2009-02-14 23:21:04 +0100 25) return git_path("rr-cache/%s/%s", hex, file);5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 26) }5b2fd956 rerere.c (Stephan Beyer 2008-07-09 14:58:57 +0200 27)

Page 229: Github - Git Training Slides: Foundations

NetworkOffline design

Page 230: Github - Git Training Slides: Foundations

Offline

‣ Local repository is a full copy of the remote

‣Clone fetches all branches and tags

‣Almost all activities happen offline (local disk)

‣Offline activities are push-ed to remotes

Page 231: Github - Git Training Slides: Foundations

More available connectivity

Page 232: Github - Git Training Slides: Foundations

More demand to work without connectivity?

Page 233: Github - Git Training Slides: Foundations

Offline

nice benefit,but wrong reason

Page 234: Github - Git Training Slides: Foundations

Offline

batched network access

Page 235: Github - Git Training Slides: Foundations

Offline

Page 236: Github - Git Training Slides: Foundations

Offline

Checkout a branchAdd changesCommit changesBranch changesLog historyGrep historyStash pending changesTag a commitRemove a fileMerge a branchRewrite history

Page 237: Github - Git Training Slides: Foundations

Offline

give up incremental revision numbers

Page 238: Github - Git Training Slides: Foundations

Offline

‣ Using every command offline

Page 239: Github - Git Training Slides: Foundations

NetworkCloning protocols

Page 240: Github - Git Training Slides: Foundations

Cloning Repositories

‣Git supports many cloning protocols‣file‣git‣ssh‣http

Page 241: Github - Git Training Slides: Foundations

Cloning Repositories

‣file‣git clone file://myrepos/project‣git clone /myrepos/project

Page 242: Github - Git Training Slides: Foundations

Cloning Repositories

‣git‣git clone git://server/project.git

Page 243: Github - Git Training Slides: Foundations

Cloning Repositories

‣ssh‣ git clone git+ssh://user@server:project.git

‣ git clone user@server:project.git

Page 244: Github - Git Training Slides: Foundations

Cloning Repositories

‣http (dumb)‣ git clone http://server/project.git‣ git clone https://server/project.git

Page 245: Github - Git Training Slides: Foundations

Cloning Repositories

‣http (smart)‣git clone http://server/project.git‣git clone https://server/project.git

Page 246: Github - Git Training Slides: Foundations

Cloning Repositories

‣ Clone hellogitworld

‣ git clone

Page 247: Github - Git Training Slides: Foundations

NetworkProxy servers

Page 248: Github - Git Training Slides: Foundations

Cloning Repositories

‣Git Configuration‣ git config --global http.proxy “<URL>”

Page 249: Github - Git Training Slides: Foundations

NetworkSpeed

Page 250: Github - Git Training Slides: Foundations

‣git: git‣hg: mercurial‣bzr: bazaar

Page 251: Github - Git Training Slides: Foundations

data from http://whygitisbetterthanx.com/#git-is-fast

git svn

Init

git svn

Status

git svn

Diff

git svn

Tag

git svn

Log

git svn

Commit (Lg)

git svn

Commit (Sm)

git c svn c

Branch

Speed

Page 252: Github - Git Training Slides: Foundations

Speed

‣ Add, commit and push 1000 files

Page 253: Github - Git Training Slides: Foundations

NetworkNamespaces

Page 254: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

Page 255: Github - Git Training Slides: Foundations

Namespaces

#List local branchesgit branch

Page 256: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

Page 257: Github - Git Training Slides: Foundations

Namespaces

#List remote branchesgit branch -r

Page 258: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

Page 259: Github - Git Training Slides: Foundations

Namespaces

#List all branchesgit branch -a

Page 260: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

Page 261: Github - Git Training Slides: Foundations

Namespaces

#List upstream branchesgit ls-remote origin

Page 262: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

Page 263: Github - Git Training Slides: Foundations

NetworkNamespace operations

Page 264: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

commit

fetch

push

clone clone clone

push

pull pull pull

Page 265: Github - Git Training Slides: Foundations

NetworkThe commit lifecycle

Page 266: Github - Git Training Slides: Foundations

Namespaces

Commit

Push

Pull

Page 267: Github - Git Training Slides: Foundations

Namespaces

‣git commit

‣ Transactionally save code snapshot‣Commit to local branch‣Operate on local disk

Page 268: Github - Git Training Slides: Foundations

Namespaces

Commit

Push

Pull

Page 269: Github - Git Training Slides: Foundations

Namespaces

‣git push <remote>

‣Send code to an upstream server‣Update remote branches

Page 270: Github - Git Training Slides: Foundations

Namespaces

Commit

Push

Pull

Page 271: Github - Git Training Slides: Foundations

Namespaces

‣git pull <remote>

‣ Retrieve upstream objects‣ Update remote branch‣Merge changes into local branch‣ Commit the merge to the local branch

Page 272: Github - Git Training Slides: Foundations

Namespaces

Local Remote Upstream

commit

fetch

push

clone clone clone

push

pull pull pull

Page 273: Github - Git Training Slides: Foundations

NetworkRemotes

Page 274: Github - Git Training Slides: Foundations

Remotes

Remotes are just symbolic names

Page 275: Github - Git Training Slides: Foundations

Remotes

You can have as many as you like

Page 276: Github - Git Training Slides: Foundations

Remotes

The default name is origin if you’ve cloned

Page 277: Github - Git Training Slides: Foundations

Remotes

Remote-tracking branches are locally immutable (conceptually)

Page 278: Github - Git Training Slides: Foundations

Remotes

Local Remote Upstream

Page 279: Github - Git Training Slides: Foundations

Remotes

‣ Adding remotes

‣ Fetching from remotes (upstream)

Page 280: Github - Git Training Slides: Foundations

NetworkPruning deletions

Page 281: Github - Git Training Slides: Foundations

‣ Purge remote branches that have been removed from an upstream repository

‣ git remote prune <REMOTENAME>

Page 282: Github - Git Training Slides: Foundations

Remotes

‣Deleting upstream branches

‣Pruning locally

Page 283: Github - Git Training Slides: Foundations

ArchitecturePlumbing and Porcelain

Page 284: Github - Git Training Slides: Foundations

Plumbing and Porcelain

Command Composition

Page 285: Github - Git Training Slides: Foundations

Plumbing is the set of low level utilities

Command Composition

Page 286: Github - Git Training Slides: Foundations

Porcelain is the set of end user commands

Command Composition

Page 287: Github - Git Training Slides: Foundations

Porcelain is comprised of plumbing

Command Composition

Page 288: Github - Git Training Slides: Foundations

Command Composition

Page 289: Github - Git Training Slides: Foundations

git-add git-am git-archive git-bisect git-branch git-bundle git-checkout git-cherry-pick git-citool git-clean git-clone git-commit

git-describe git-diff git-fetch git-format-patch git-gc git-grep git-gui git-init git-log git-merge git-mv git-notes git-pull git-push git-

rebase git-reset git-revert git-rm git-shortlog git-show git-stash git-status git-submodule git-tag gitk git-config git-fast-export git-fast-import git-filter-branch git-lost-found git-mergetool git-pack-refs git-prune git-reflog git-relink git-remote git-repack git-replace git-repo-config git-annotate git-blame git-cherry git-count-objects

git-difftool git-fsck git-get-tar-commit-id git-help git-instaweb git-merge-tree git-rerere git-rev-parse git-show-branch git-verify-tag

git-whatchanged git-archimport git-cvsexportcommit git-cvsimport git-cvsserver git-imap-send git-quiltimport git-request-pull git-send-

email git-svnPo

rcel

ain

git-apply git-checkout-index git-commit-tree git-hash-object git-index-pack git-merge-file git-merge-index git-mktag git-mktree git-pack-objects git-prune-packed git-read-tree git-

symbolic-ref git-unpack-objects git-update-index git-update-ref git-write-tree git-cat-file git-diff-files git-diff-index git-diff-tree git-for-each-ref git-ls-files git-ls-remote git-ls-tree git-merge-base git-name-rev git-pack-redundant git-rev-list git-show-index git-show-ref git-tar-tree git-unpack-file git-var git-verify-pack git-daemon git-fetch-pack git-http-backend git-send-pack git-update-server-info git-http-fetch git-http-push git-parse-remote git-receive-pack git-shell git-upload-archive git-upload-pack git-check-attr git-check-ref-

format git-fmt-merge-msg git-mailinfo git-mailsplit git-merge-one-file git-patch-id git-peek-remote git-sh-setup git-stripspace

Plum

bing

Page 290: Github - Git Training Slides: Foundations

pull is comprised of fetch + merge

Command Composition

Page 291: Github - Git Training Slides: Foundations

checkout -b is comprised of branch + checkout

Command Composition

Page 292: Github - Git Training Slides: Foundations

log HEAD is comprised of rev-parse + log

Command Composition

Page 293: Github - Git Training Slides: Foundations

AliasesCommand shortcuts

Page 294: Github - Git Training Slides: Foundations

Aliases

‣Shortcuts for common commands

‣Create your own recipe

‣Largely the same as shell aliases

Page 295: Github - Git Training Slides: Foundations

Aliases

# Alias for status as 's'git config --global alias.s "status -u -s"

Page 296: Github - Git Training Slides: Foundations

Aliases

‣ Add an alias

Page 297: Github - Git Training Slides: Foundations

Aliases

# Alias for log with file name as 'l'git config --global alias.l "log --stat -C"

Page 298: Github - Git Training Slides: Foundations

Aliases

# Alias showing all branches to 'br'git config --global alias.br "branch -a"

Page 299: Github - Git Training Slides: Foundations

Aliases

# Alias for commit-no-staging to 'cns'git config --global alias.cns "commit -a"

Page 300: Github - Git Training Slides: Foundations

Aliases

# Alias for shell pull then push to 'sync'$ git config --global alias.sync "!git pull && git push"

Page 301: Github - Git Training Slides: Foundations

Aliases

# Alias for crummy-commit-file-quick$ git config --global alias.ccfq "!sh -c 'git add $1 && git commit -m\"Placeholder\"' -"

Page 302: Github - Git Training Slides: Foundations

Aliases

‣ Matthew's Aliases

‣ https://github.com/matthewmccullough/git-workshop/blob/master/workbook/examples/config/.gitconfig

Page 303: Github - Git Training Slides: Foundations

ArchitectureStorage

Page 304: Github - Git Training Slides: Foundations

Typical SCMs use delta storage

Storage

Page 305: Github - Git Training Slides: Foundations

CVS / Subversion / darcs / Mercurial

Storage

Page 306: Github - Git Training Slides: Foundations

Storage

v1 v2 v3 v4

File A

File B

File C

File A

File B File B

File C

v5

File A

File BFile B

Δ Δ

ΔΔ ΔΔΔ

Page 307: Github - Git Training Slides: Foundations

Checkin

Checkin Checkin Che

ckin

Che

ckin

Che

ckin

Che

ckin

Che

ckin

Check

in

Checkin

Checkin

Delta storage gets slower as the history of a file gets longer

Storage

Page 308: Github - Git Training Slides: Foundations

Git uses DAG storage

Storage

Page 309: Github - Git Training Slides: Foundations

Directed Acyclic Graph

Storage

Page 310: Github - Git Training Slides: Foundations

Storage

Page 311: Github - Git Training Slides: Foundations

Copy of the entire tree per checkin

Storage

Page 312: Github - Git Training Slides: Foundations

cp -r srcfolder srcfolder.prev

Storage

Page 313: Github - Git Training Slides: Foundations

Why?

Storage

Page 314: Github - Git Training Slides: Foundations

Storage

v1 v2 v3 v4

File A

File B

File C

File A

File B File B

File C

v5

File A

File BFile B

File A File A

File C File C File C

Page 315: Github - Git Training Slides: Foundations

hard link to existing identical blobs

Storage

Page 316: Github - Git Training Slides: Foundations

Storage

v1 v2 v3 v4

File A

File B

File C

File A

File B File B

File C

v5

File A

File BFile B

File A File A

File C File C File Cß

Page 317: Github - Git Training Slides: Foundations

zlib deflates each blob at commit

Storage

Page 318: Github - Git Training Slides: Foundations

Storage

v1 v2 v3 v4

File A

File B

File C

File A

File B File B

File C

v5

File A

File BFile B

File A File A

File C File C File C

Page 319: Github - Git Training Slides: Foundations

zlib deflates the entire repo

Storage

Page 320: Github - Git Training Slides: Foundations

Storage

v1 v2 v3 v4

File A

File B

File C

File A

File B File B

File C

v5

File A

File BFile B

File A File A

File C File C File C

Page 321: Github - Git Training Slides: Foundations

Storage

2100 MB became 205 MB

Act I

Page 322: Github - Git Training Slides: Foundations

ArchitectureHashes

Page 323: Github - Git Training Slides: Foundations

Hashes

centralized VCSs use sequential revision numbers

Page 324: Github - Git Training Slides: Foundations

Hashes

Git uses a SHA-1 hash

Page 325: Github - Git Training Slides: Foundations

Hashes

Page 326: Github - Git Training Slides: Foundations

40 hex characters (20 bytes)

Hashes

Page 327: Github - Git Training Slides: Foundations

9AB223D28B1AA46EF1780B22F304982E39872C34

Hashes

Page 328: Github - Git Training Slides: Foundations

Hashes

9AB223D28B1AA46EF1780B22F304982E39872C34

<html><body>

<p>This is a test</p><img src="http://ai.com/icon.gif">

</body></html>

9AB223D28B1AA46EF1780B22F304982E39872C34

Page 329: Github - Git Training Slides: Foundations

use as little of it as is unique

Hashes

Page 330: Github - Git Training Slides: Foundations

ArchitectureHash shortcuts

Page 331: Github - Git Training Slides: Foundations

commitish & treeish

Hashes

Page 332: Github - Git Training Slides: Foundations

commitish

Hashes

= shorthand for commit hashes

Page 333: Github - Git Training Slides: Foundations

treeish

Hashes

= shorthand for tree hashes

Page 334: Github - Git Training Slides: Foundations

9AB22Fa certain commit

Hashes

Page 335: Github - Git Training Slides: Foundations

9AB22F^one commit before a certain commit

Hashes

Page 336: Github - Git Training Slides: Foundations

9AB22F^^two commits before a certain commit

Hashes

Page 337: Github - Git Training Slides: Foundations

9AB22F~5five commits before a certain commit

Hashes

Page 338: Github - Git Training Slides: Foundations

9AB223..56CD77between these two commits

Hashes

Page 339: Github - Git Training Slides: Foundations

HEADthe most recent commit on this branch

Hashes

Page 340: Github - Git Training Slides: Foundations

HEAD^one commit before the most recent commit

Hashes

Page 341: Github - Git Training Slides: Foundations

HEAD~2two commits before the most recent commit

Hashes

Page 342: Github - Git Training Slides: Foundations

HEAD..HEAD^^^between the given recent commits

Hashes

Page 343: Github - Git Training Slides: Foundations

masterthe most recent commit on this branch

Hashes

Page 344: Github - Git Training Slides: Foundations

master^^two commits before the most recent commit on this branch

Hashes

Page 345: Github - Git Training Slides: Foundations

master~5five commits before the most recent commit on this branch

Hashes

Page 346: Github - Git Training Slides: Foundations

remotes/origin/masterthe most recent commit on this remote tracking branch

Hashes

Page 347: Github - Git Training Slides: Foundations

origin/masterthe most recent commit on this remote tracking branch

Hashes

Page 348: Github - Git Training Slides: Foundations

Hashes

Navigate with commitish oncommit, status, & log

Page 349: Github - Git Training Slides: Foundations

ArchitectureHash relationships

Page 350: Github - Git Training Slides: Foundations

Hashes

‣Blob‣Tree‣Commit‣Tag

Page 351: Github - Git Training Slides: Foundations

Hashes

treetree: 7e8b1 webblob: 9ab16 index.html

a10b3 treeblob: 8d162 logo.jpgblob: 51d22 draw.js

7e8b1commit

tree: a10b3parent: nilauthor: Firdcommitter: Matthewmessage:

Major refactoringof the web content.

c67db

blob<html>

<body></body></html>

9ab16

blob//Some more javascriptvar renderSize

51d22

blob7D 8D B3 7F BD 12 9F E97B 78 9D 3F 5C A6 72 CB

8d162

Page 352: Github - Git Training Slides: Foundations

Hashes

v1 v2 v3

committree: 9a87bparent: nilauthor: Firdcommitter: Matthewmessage:

Major refactoringof the Javascript renderingengine.

c67db

committree: b22c1parent: c67dbauthor: Timcommitter: Firdmessage:

Minor update to HTML

9bd21

committree: b22c1parent: 9bd21author: Johnnycommitter: Joemessage:

New language transations

1c2d7

Page 353: Github - Git Training Slides: Foundations

Hashes

RELEASE_1.0 HEAD

bug979branch

commitc67db

commit9bd21

commit1c2d7

commit8c2d1

commit1bdcd

commit2daa1

Page 354: Github - Git Training Slides: Foundations

BranchingCreating branches

Page 355: Github - Git Training Slides: Foundations

Branches

default branch name is master

Page 356: Github - Git Training Slides: Foundations

Branches

master doesn’t have any special privileges

Page 357: Github - Git Training Slides: Foundations

Branches

Local Remote Upstream

Page 358: Github - Git Training Slides: Foundations

Branches

# Creating a branchgit branch <BRANCHNAME>

Page 359: Github - Git Training Slides: Foundations

RELEASE_1.0 HEAD

bug979branch

commitc67db

commit9bd21

commit1c2d7

commit8c2d1

commit1bdcd

commit2daa1

Page 360: Github - Git Training Slides: Foundations

Hashes

‣Create a new local branch from HEAD

Page 361: Github - Git Training Slides: Foundations

Branches

# Creating a branchgit branch <BRANCHNAME> HEAD

Page 362: Github - Git Training Slides: Foundations

Branches

# Creating a branchgit branch <BRANCHNAME> <REF>

Page 363: Github - Git Training Slides: Foundations

RELEASE_1.0 HEAD

bug979branch

commitc67db

commit9bd21

commit1c2d7

commit8c2d1

commit1bdcd

commit2daa1

Page 364: Github - Git Training Slides: Foundations

Hashes

‣Create a new local branch from a branch

Page 365: Github - Git Training Slides: Foundations

BranchingBranch as experiments

Page 366: Github - Git Training Slides: Foundations

Branches

What do cheap branches enable?

Page 367: Github - Git Training Slides: Foundations

Branches

‣Experimentation

Page 368: Github - Git Training Slides: Foundations

Branches

‣Experimentation

‣Safe experimentation

Page 369: Github - Git Training Slides: Foundations

Branches

Better reuse of units of work

Page 370: Github - Git Training Slides: Foundations

Hashes

‣Create a branch for an experiment

‣Delete the failed experiment

Page 371: Github - Git Training Slides: Foundations

BranchingBranch frequency

Page 372: Github - Git Training Slides: Foundations

Branches

When should you branch?

Page 373: Github - Git Training Slides: Foundations

Branches

The answer is always

Page 374: Github - Git Training Slides: Foundations

Branches

Branches isolate volatile work

Page 375: Github - Git Training Slides: Foundations

Branches

Branches cost a mere 20 bytes

Page 376: Github - Git Training Slides: Foundations

Branches

We’ve always wanted to branch oftenIt’s just been too expensive, polluting, or ceremonious

Page 377: Github - Git Training Slides: Foundations

Hashes

‣Show branches on the Git project

Page 378: Github - Git Training Slides: Foundations

BranchingBranch lifetimes

Page 379: Github - Git Training Slides: Foundations

Branches

‣Branched by lifetime‣ Product‣ Integration‣ Feature‣ Story‣ Idea

Page 380: Github - Git Training Slides: Foundations

Branches

product

integration

feature

story

idea

Page 381: Github - Git Training Slides: Foundations

product

v1.0 integration

story1 story2

v1.5 integration

story3

Branches

Page 382: Github - Git Training Slides: Foundations

Branches

Local Remote Upstream

Integ

Product

Integ

Product

Integ

Product

Page 383: Github - Git Training Slides: Foundations

Branches

Local Remote Upstream

Idea

Story

Feature FeatureFeature

Integ

Product

Integ

Product

Integ

Product

Page 384: Github - Git Training Slides: Foundations

Branches

Local Remote Upstream

Idea

Story

Feature FeatureFeature

Integ

Product

Integ

Product

Integ

Product

Page 385: Github - Git Training Slides: Foundations

Branches

‣Branch from the master branch

‣Merge from the topic branch

‣Destroy the topic branch

Page 386: Github - Git Training Slides: Foundations

BranchingStashes

Page 387: Github - Git Training Slides: Foundations

Stash

super quick branch

Page 388: Github - Git Training Slides: Foundations

Stash

local-only branch

Page 389: Github - Git Training Slides: Foundations

Stash

‣Numbered branch‣Stack based implementation‣Push, Peek, and Pop operations

(But has direct entry access too)

Page 390: Github - Git Training Slides: Foundations

Stash

creating a stash

Page 391: Github - Git Training Slides: Foundations

Stash

# Stash your pending changesgit stash

Page 392: Github - Git Training Slides: Foundations

Stash

inspecting the stash

Page 393: Github - Git Training Slides: Foundations

Stash

# List your stashesgit stash list

Page 394: Github - Git Training Slides: Foundations

Stash

noting the stash

Page 395: Github - Git Training Slides: Foundations

Stash

# Stash your pending changesgit stash save “<Message>”

Page 396: Github - Git Training Slides: Foundations

Stash

# List your stashesgit stash list

Page 397: Github - Git Training Slides: Foundations

Stash

using the stash

Page 398: Github - Git Training Slides: Foundations

Stash

# Merge & delete the latest stashgit stash pop

Page 399: Github - Git Training Slides: Foundations

Stash

# Merge & delete a stashgit stash pop stash@{0}

Page 400: Github - Git Training Slides: Foundations

Stash

# Merge & keep the latest stashgit stash apply

Page 401: Github - Git Training Slides: Foundations

Branches

‣Stash modified changes

‣Stash staged changes

‣Apply stashed changes

Page 402: Github - Git Training Slides: Foundations

Stash

converting a stash

Page 403: Github - Git Training Slides: Foundations

Stash

# Convert a stash to a branchgit stash branch <newbr>

Page 404: Github - Git Training Slides: Foundations

Stash

# Convert a stash to a branchgit stash branch <newbr> stash@{3}

Page 405: Github - Git Training Slides: Foundations

Branches

‣Convert a stash to a branch

Page 406: Github - Git Training Slides: Foundations

TaggingTag uses

Page 407: Github - Git Training Slides: Foundations

tags as a first class data type

Page 408: Github - Git Training Slides: Foundations

tagging as a cheap operation

Page 409: Github - Git Training Slides: Foundations

New ways to use tags?

Page 410: Github - Git Training Slides: Foundations

Tagging

‣Tagging at each level of approval‣Dev‣CM‣QA‣Production

Page 411: Github - Git Training Slides: Foundations

TaggingTag types

Page 412: Github - Git Training Slides: Foundations

Tagging

reference, annotated and signed tag types

Page 413: Github - Git Training Slides: Foundations

TaggingReference tag

Page 414: Github - Git Training Slides: Foundations

Tagging

reference tag...

Page 415: Github - Git Training Slides: Foundations

Tagging

# Tag HEADgit tag <TAGNAME>

Page 416: Github - Git Training Slides: Foundations

Tagging

# Tag an existing refgit tag <TAGNAME> <REF>

Page 417: Github - Git Training Slides: Foundations

Tagging

# List known tagsgit tag

Page 418: Github - Git Training Slides: Foundations

Tagging

# Show a tag’s contentsgit show tag

Page 419: Github - Git Training Slides: Foundations
Page 420: Github - Git Training Slides: Foundations

Tagging

‣Tag a revision

‣Start a branch from a tag

Page 421: Github - Git Training Slides: Foundations

TaggingAnnotated tag

Page 422: Github - Git Training Slides: Foundations

Tagging

annotated tag...

Page 423: Github - Git Training Slides: Foundations

Tagging

git tag -a <TAGNAME>

Page 424: Github - Git Training Slides: Foundations
Page 425: Github - Git Training Slides: Foundations

Tagging

git show <TAGNAME>

Page 426: Github - Git Training Slides: Foundations

Tagging

‣Tag a revision with an annotated tag

Page 427: Github - Git Training Slides: Foundations

TaggingSigned tag

Page 428: Github - Git Training Slides: Foundations

Tagging

signed tag...

Page 429: Github - Git Training Slides: Foundations

Tagging

git tag -s <TAGNAME>

Page 430: Github - Git Training Slides: Foundations
Page 431: Github - Git Training Slides: Foundations

Tagging

git show <TAGNAME>

Page 432: Github - Git Training Slides: Foundations

Tagging

‣Tag a revision with a signed tag

Page 433: Github - Git Training Slides: Foundations

TaggingTransmitting tags

Page 434: Github - Git Training Slides: Foundations

Tagging

Tags don't push by default

Page 435: Github - Git Training Slides: Foundations

Tagging

# Push all tagsgit push <remote> <tag>

Page 436: Github - Git Training Slides: Foundations

Tagging

# Push all tagsgit push --tags

Page 437: Github - Git Training Slides: Foundations

‣Push a specific tag‣Push all tags

Page 438: Github - Git Training Slides: Foundations

Tagging

Tags do fetch by default

Page 439: Github - Git Training Slides: Foundations

‣Fetch all tags

Page 440: Github - Git Training Slides: Foundations

Tagging

but the refspec doesn’t say to

Page 441: Github - Git Training Slides: Foundations

‣Inspect .git/config refspec

Page 442: Github - Git Training Slides: Foundations

MergingThe basics

Page 443: Github - Git Training Slides: Foundations

Merging

very traditional merge of a branch

Page 444: Github - Git Training Slides: Foundations

Merging

git checkout mastergit merge <featurebranch>

Page 445: Github - Git Training Slides: Foundations

a32

2e2

e69

d19

8b3

Branch

Mas

ter/T

runk

/Mai

nLat

est

Merging

Recursive

Merge

9f1 Mergee698b3

Page 446: Github - Git Training Slides: Foundations

strategy: recursiveresult: no conflicts

Page 447: Github - Git Training Slides: Foundations

a32

e69

d19

Branch

Mas

ter/T

runk

/Mai

nLat

est

Merging

Fast Forward

Merge

Merge

Page 448: Github - Git Training Slides: Foundations

a32

e69

d19

Mas

ter/T

runk

/Mai

nLat

est

Merging

FF

Merge

Page 449: Github - Git Training Slides: Foundations

strategy: recursiveresult: fast forward

Page 450: Github - Git Training Slides: Foundations

a32

2e2

e69

d19

8b3

Branch

Mas

ter/T

runk

/Mai

nLat

est

Merging

Conflicting

Merge

Merge

9f1e698b3

Fix Conflict

Page 451: Github - Git Training Slides: Foundations

strategy: recursiveresult: conflicting

Page 452: Github - Git Training Slides: Foundations

MergingOctopus

Page 453: Github - Git Training Slides: Foundations

Merging

git checkout mastergit merge <fb1> <fb2> <fb3>

Page 454: Github - Git Training Slides: Foundations

strategy: octopus

Page 455: Github - Git Training Slides: Foundations

MergingSubtree

Page 456: Github - Git Training Slides: Foundations

Merging

git checkout mastergit merge -s subtree <fb1>

Page 457: Github - Git Training Slides: Foundations

Merging

git checkout mastergit merge --squash -s subtree <fb1>

Page 458: Github - Git Training Slides: Foundations

strategy: subtree

Page 459: Github - Git Training Slides: Foundations

Tagging

‣Merge a local branch‣Merge a remote branch

Page 460: Github - Git Training Slides: Foundations

RebasingWhat is rebasing?

Page 461: Github - Git Training Slides: Foundations

rebasing is not a merge

Rebasing

Page 462: Github - Git Training Slides: Foundations

rebasing is a preparation for a merge

Rebasing

Page 463: Github - Git Training Slides: Foundations

Merges weave multiple old changes intoa new unifying commit

Rebasing

Page 464: Github - Git Training Slides: Foundations

Rebase reorders the chosen commits before your branch work

Rebasing

Page 465: Github - Git Training Slides: Foundations

simulates team members taking turns working(one person at a time)

Rebasing

Page 466: Github - Git Training Slides: Foundations

RebasingRebasing on a branch

Page 467: Github - Git Training Slides: Foundations

Rebasing

git pull --rebase

Page 468: Github - Git Training Slides: Foundations

Retrieve upstream changes and relocate your local changes to the end

Rebasing

Page 469: Github - Git Training Slides: Foundations

Rebasing

git pull --rebase

# is the same asgit checkout mastergit rebase origin/master

Page 470: Github - Git Training Slides: Foundations

Rebasing

git checkout <featurebranch>git rebase master

Page 471: Github - Git Training Slides: Foundations

a32

2e2

8b3

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Branch

Page 472: Github - Git Training Slides: Foundations

a32

2e2

8b3

e69

d19

Branch

Mas

ter/

Trun

k/M

ainL

ates

t

Rebase

Rebasing

Page 473: Github - Git Training Slides: Foundations

a32

2e2

e69'

d19'

8b3 Branch

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

Rebase

Page 474: Github - Git Training Slides: Foundations

a32

2e2

e69'

d19'

8b3 Branch

Mas

ter/

Trun

k/M

ainL

ates

t

Merge

Rebasing

Page 475: Github - Git Training Slides: Foundations

Rebasing

‣Rebase topic branch on master

Page 476: Github - Git Training Slides: Foundations

RebasingInteractive rebasing

Page 477: Github - Git Training Slides: Foundations

Rework your work so it makes sense to the team

Rebasing

Page 478: Github - Git Training Slides: Foundations

Rebasing

git checkout myfeaturebranch# Replay the last 5 commitsgit rebase -i HEAD~5

Page 479: Github - Git Training Slides: Foundations

a32

2e2

8b3

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 480: Github - Git Training Slides: Foundations

a32

2e2

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 481: Github - Git Training Slides: Foundations

a32

2e2

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 482: Github - Git Training Slides: Foundations

a32

2e2

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 483: Github - Git Training Slides: Foundations

a32

2e2

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 484: Github - Git Training Slides: Foundations

a32

2e2

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

Page 485: Github - Git Training Slides: Foundations

a32

Mas

ter/

Trun

k/M

ainL

ates

t

Rebasing

e69

d19

2e2

Page 486: Github - Git Training Slides: Foundations

Rebasing

‣Interactively rebase a single branch

Page 487: Github - Git Training Slides: Foundations

WorkflowsUsage Models

Page 488: Github - Git Training Slides: Foundations

Usage Models

Central Repo

Ce

nt

ra

liz

ed

Page 489: Github - Git Training Slides: Foundations

Usage Models

Blessed Repo

Dic

ta

to

rs

hip

Page 490: Github - Git Training Slides: Foundations

Usage Models

Certified Repo

Development Repo

Int

eg

ra

tio

n M

an

ag

ed

ContinuousIntegration

Server

Page 491: Github - Git Training Slides: Foundations

Usage Models

Cu

st

om

+ P

ub

lic

Co

nt

rib

Customized

� P

riva

te

Pub

lic �

GitHub

Page 492: Github - Git Training Slides: Foundations

Usage Models

Mirror

Development Repo

Mir

ro

re

d

Mirror

Certified Repo

Page 493: Github - Git Training Slides: Foundations

UndoClean

Page 494: Github - Git Training Slides: Foundations

Clean

clean purges untracked files

respects ignored and tracked

Page 495: Github - Git Training Slides: Foundations

Clean

# Dry-run remove filesgit clean -n

Page 496: Github - Git Training Slides: Foundations

Clean

# Dry-run remove files, dirsgit clean -nd

Page 497: Github - Git Training Slides: Foundations

Clean

# Remove filesgit clean -f

Page 498: Github - Git Training Slides: Foundations

Clean

# Remove files, dirsgit clean -fd

Page 499: Github - Git Training Slides: Foundations

Undo

‣Clean untracked files

Page 500: Github - Git Training Slides: Foundations

Clean

# Also remove ignored files

git clean -xf

Page 501: Github - Git Training Slides: Foundations

Clean

# Also remove ignored files, dirs

git clean -xdf

Page 502: Github - Git Training Slides: Foundations

Clean

# Only list ignored files

git clean -Xn

Page 503: Github - Git Training Slides: Foundations

Clean

# Only remove ignored files

git clean -Xf

Page 504: Github - Git Training Slides: Foundations

Undo

‣Clean ignored files

Page 505: Github - Git Training Slides: Foundations

UndoRevert

Page 506: Github - Git Training Slides: Foundations

Revert

revert negates one or more commits

Page 507: Github - Git Training Slides: Foundations

Revert

new commit at the end of HEAD

Page 508: Github - Git Training Slides: Foundations

Revert

no pointer to old ref in revert commit

Page 509: Github - Git Training Slides: Foundations

Revert

comment references the old one

Page 510: Github - Git Training Slides: Foundations

8b3 '

Revert

a32

2e2

d19'

8b3

Page 511: Github - Git Training Slides: Foundations

Revert

a32

2e2

d19'

8b3

Revert

8b3 '

Page 512: Github - Git Training Slides: Foundations

Revert

# Revert a single commitgit revert <ref>

Page 513: Github - Git Training Slides: Foundations

Revert

‣Revert a single change

Page 514: Github - Git Training Slides: Foundations

Revert

# Revert a range of commitsgit revert <ref1>..<ref2>

Page 515: Github - Git Training Slides: Foundations

Revert

# Revert a range of commitsgit revert <old>..<new>

Page 516: Github - Git Training Slides: Foundations

Revert

must have the old..new refs

in the right order

Page 517: Github - Git Training Slides: Foundations

UndoAmend

Page 518: Github - Git Training Slides: Foundations

Amend

amend rewrites the last commit

Page 519: Github - Git Training Slides: Foundations

Amend

git commit --amend

Page 520: Github - Git Training Slides: Foundations

Amend

‣Amend a bad commit message

Page 521: Github - Git Training Slides: Foundations

Amend

git add <missingfile>git commit --amend

Page 522: Github - Git Training Slides: Foundations

Amend

‣Amend a missing file

Page 523: Github - Git Training Slides: Foundations

Git-SVNCloning

Page 524: Github - Git Training Slides: Foundations

git-svn

‣Subversion protocol bridge from Git

‣Round-trip integration

‣Transactions in Git == transactions in SVN

Page 525: Github - Git Training Slides: Foundations

git-svn

# Clone one branchgit svn clone <svnurl>

Page 526: Github - Git Training Slides: Foundations

git-svn

# Clone all branches, tagsgit svn clone --stdlayout <svnurl>

Page 527: Github - Git Training Slides: Foundations

git-svn

‣ Alternate conversion tool‣ svn2git

‣ https://github.com/nirvdrum/svn2git

‣ Converts tags to Git tags

Page 528: Github - Git Training Slides: Foundations

Git-SVNUpdating

Page 529: Github - Git Training Slides: Foundations

git-svn

‣Fetch new changes‣git svn rebase

Page 530: Github - Git Training Slides: Foundations

git-svn

‣Send new changes‣git svn dcommit

Page 531: Github - Git Training Slides: Foundations

Git-SVNSVN Externals

Page 532: Github - Git Training Slides: Foundations

git-svn

‣ SVN Externals

‣ No direct support

‣ Represented as separate repos

‣ Git points at a stable snapshot

‣ SVN Externals follow changes on a branch

Page 533: Github - Git Training Slides: Foundations

git-svn

‣Externals cloning process‣ svn propget svn:externals <MODULE>

‣ git svn clone --stdlayout <THEURL>

‣ git submodule add <THESUBFOLDER>

‣ git submodule init

‣ git submodule update

Page 534: Github - Git Training Slides: Foundations

git-svn

‣ SVN Externals helper script

‣ https://github.com/andrep/git-svn-clone-externals

Page 535: Github - Git Training Slides: Foundations

Git FoundationsAn exploration of the Git toolbox

Offi

cial

trai

ning

cur

ricul

um v

3.1.

201

2, G

itHub

, Inc

.

Page 537: Github - Git Training Slides: Foundations

Credits

‣ Images sourced from:‣ AmbientIdeasPhotography.com‣ Hand Tools

‣ Flickr Creative Commons‣ Clock: http://www.flickr.com/photos/7729940@N06/4019157830

‣ Wikipedia‣ Linus Torvalds: http://en.wikipedia.org/wiki/File:Linus_Torvalds.jpeg