Everything you ever wanted to know about Git (But were afraid to ask)

68
Everything you wanted to know about GIT (BUT WERE AFRAID TO ASK)

Transcript of Everything you ever wanted to know about Git (But were afraid to ask)

Page 1: Everything you ever wanted to know about Git (But were afraid to ask)

Everything you wanted to know about GIT(BUT WERE AFRAID TO ASK)

Page 2: Everything you ever wanted to know about Git (But were afraid to ask)

ANIMATIONS AHEAD!!!

This deck has plenty of animations to explain some concepts.

Please view in presentation mode

Page 3: Everything you ever wanted to know about Git (But were afraid to ask)

Setting up

Apple GIT gotchas:Xcode ships with its custom version of git. You don’t want this:

$ git --version

git version 2.3.2 (Apple Git-55)

$ which git

/usr/bin/git

Page 4: Everything you ever wanted to know about Git (But were afraid to ask)

Setting Up

The right git: Download and install:

http://git-scm.com/download/mac

Set up:Modify ~/.profileexport PATH=/usr/local/bin:$PATH

$ git --version

git version 2.4.3

Page 5: Everything you ever wanted to know about Git (But were afraid to ask)

Setting Up

Make life easy for yourself: Autocomplete!Download: https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bashSave to -> ~/.git-completion.bash (Note the dot!)Add to ~/.profile

# Run git autocomplete source ~/.git-completion.bash

Verify -> git check<tab> --autocompletes to--> git checkout

PRO TIP: Also autocompletes branch names!

Page 6: Everything you ever wanted to know about Git (But were afraid to ask)

Setting Up

Make life easy for yourself, PART 2: Change your terminal prompt!Download: https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.shSave to -> ~/.git-prompt.sh (Note the dot!)Add to ~/.profile

# Run git prompt enhancementssource ~/.git-prompt.shexport GIT_PS1_SHOWDIRTYSTATE=trueexport GIT_PS1_SHOWUNTRACKEDFILES=trueexport GIT_PS1_SHOWCOLORHINTS=trueexport PROMPT_COMMAND='__git_ps1 "\w" "\\\$ "'

Page 7: Everything you ever wanted to know about Git (But were afraid to ask)

GIT InitGIT IS MISUNDERSTOOD. IT JUST WANTS TO BE YOUR FRIEND

Page 8: Everything you ever wanted to know about Git (But were afraid to ask)

Git isn’t magic.

BLOBS TREES COMMITS

Page 9: Everything you ever wanted to know about Git (But were afraid to ask)

Git tracks everything. Forever!

Files (Blobs) are tracked based on content, not on their name. Git uses the SHA1 algorithm to create a unique hash (fingerprint) for each

file.

foo.txt -> “Hello” -> e965047ad7c57865823c7d992b1d046ea66edf78

Page 10: Everything you ever wanted to know about Git (But were afraid to ask)

How is it calculated?

“blob”+space

Do it yourself! printf ”blob 6\000Hello\n" | shasum

Null char

Length of content File content

blob 6\000Hello\n

Page 11: Everything you ever wanted to know about Git (But were afraid to ask)

All versions of a file end up as blobs.

E9650Hello

980a0Hello World!

53627Hello World! How are you?

7a27fHello World! How are you today?

557dbHello World

Page 12: Everything you ever wanted to know about Git (But were afraid to ask)

Trees organize (and name) sets of files

E9650Hello

a69b2

a.txt 557dbHello World

8984f

a.txt

Page 13: Everything you ever wanted to know about Git (But were afraid to ask)

Commit ec445 Commit bd297

Commits give order to trees

E9650Hello

Tree: a69b2Author: lmarkusMessage: First Commit

a.txt 557dbHello World

bd297

a.txt

ec445

Tree: 8984fParent: ec445Author: lmarkusMessage: Second Commit

Page 14: Everything you ever wanted to know about Git (But were afraid to ask)
Page 15: Everything you ever wanted to know about Git (But were afraid to ask)

Git Recap

Git saves *everything* Git keeps track of what your project looked like at a given time (tree + commit) Git keeps a history of the various snapshots as a linked list. (commit -> parent)

Page 16: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

Page 17: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 18: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git statusOn branch masternothing to commit, working directory clean

Page 19: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 20: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git statusOn branch masterChanges 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: miner.txt

no changes added to commit (use "git add" and/or "git commit -a")

Page 21: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git add miner.txt

Page 22: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 23: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)

modified: miner.txt

Page 24: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 25: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 26: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)

modified: miner.txt

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

sun.txt

Page 27: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

$ git commit -m “Mining is hard”

Page 28: Everything you ever wanted to know about Git (But were afraid to ask)

The world is my stage!

HEAD

STAGE

Work Dir

Page 29: Everything you ever wanted to know about Git (But were afraid to ask)

“Oops…” moments

Page 30: Everything you ever wanted to know about Git (But were afraid to ask)

Git Reset

A B C D E

STAGE WORKING DIR

E E

HEAD

Page 31: Everything you ever wanted to know about Git (But were afraid to ask)

Git Reset --soft

A B C D E

STAGE WORKING DIR

E E

HEAD

git reset --soft HEAD~1

Page 32: Everything you ever wanted to know about Git (But were afraid to ask)

Git Reset --mixed

A B C D E

STAGE WORKING DIR

D E

HEAD

git reset --mixed HEAD~1

E

Page 33: Everything you ever wanted to know about Git (But were afraid to ask)

Git Reset --hard

A B C D E

STAGE WORKING DIR

D D

HEAD

git reset --hard HEAD~1

E E

Page 34: Everything you ever wanted to know about Git (But were afraid to ask)

Clean up after yourself!

Page 35: Everything you ever wanted to know about Git (But were afraid to ask)

Git Rebase - Part I

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

Page 36: Everything you ever wanted to know about Git (But were afraid to ask)

Git Rebase - Part I

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

$ git log --oneline845c2a5 Actually, that wasn't a bug. Reverting10ae9b3 Oops, fixed a bug5c68b04 Fixed Another typo83775b2 Fixed Typo099d98e Created New Function

Page 37: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

$ git rebase -i HEAD~4pick 83775b2 Fixed Typopick 5c68b04 Fixed Another typopick 10ae9b3 Oops, fixed a bugpick 845c2a5 Actually, that wasn't a bug. Reverting

# Rebase 099d98e..845c2a5 onto 099d98e (4 command(s))## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell## These lines can be re-ordered; they are executed from top to bottom.# If you remove a line here THAT COMMIT WILL BE LOST.# However, if you remove everything, the rebase will be aborted.# Note that empty commits are commented out

Page 38: Everything you ever wanted to know about Git (But were afraid to ask)

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

pick 83775b2 Fixed Typopick 5c68b04 Fixed Another typopick 10ae9b3 Oops, fixed a bugpick 845c2a5 Actually, that wasn't a bug. Reverting

squash

Git rebase --interactive

Page 39: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

pick 83775b2 Fixed Typosquash 5c68b04 Fixed Another typo

Page 40: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

pick 83775b2 Fixed Typosquash 5c68b04 Fixed Another typo

Page 41: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

# This is a combination of 2 commits.# The first commit's message is:

Fixed Typo

# This is the 2nd commit message:

Fixed Another typo

# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.

Page 42: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

# This is a combination of 2 commits.# The first commit's message is:

Documentation updates

# This is the 2nd commit message:

# Fixed Another typo

# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.

Page 43: Everything you ever wanted to know about Git (But were afraid to ask)

Git rebase --interactive

A B C D E

CreatedNew Function

Fixed typo Fixed Another typo

Oops!Fixed a bug.

Actually, that wasn’t a bug.

Reverting.

$ git log --oneline779dbd5 Documentation updates099d98e Created New Function

Page 44: Everything you ever wanted to know about Git (But were afraid to ask)

Branching out

Page 45: Everything you ever wanted to know about Git (But were afraid to ask)

Branches

A B C

MASTER

Remember: Your git history is just a linked list of commits.

A branch simply represents a marker somewhere in this history. It points to the latest commit within a set.

Page 46: Everything you ever wanted to know about Git (But were afraid to ask)

Branches

A B C

MASTER

DEVELOP

git checkout -b develop

Page 47: Everything you ever wanted to know about Git (But were afraid to ask)

Branches

A B C

MASTER

D E

DEVELOP

git commit -m ”D”git commit -m ”E”

Page 48: Everything you ever wanted to know about Git (But were afraid to ask)

Branches

A B C

MASTER

D E

DEVELOP

git checkout master

Page 49: Everything you ever wanted to know about Git (But were afraid to ask)

Branches

A B C

MASTER

D E

DEVELOP

git commit -m “F”

F

Page 50: Everything you ever wanted to know about Git (But were afraid to ask)

Rebase Vs Merge

A B C

MASTER

D E

DEVELOP

F

Page 51: Everything you ever wanted to know about Git (But were afraid to ask)

Merge

A B C

MASTER

D E

DEVELOP

F M Merge commits have TWO parents. They represent the point where to branches became one

git merge develop

X Y

Work can continue after a merge

Page 52: Everything you ever wanted to know about Git (But were afraid to ask)

Rebase

A B C

D E

DEVELOP

MASTER

FWe just re-wrote history!

git rebase develop

Page 53: Everything you ever wanted to know about Git (But were afraid to ask)

Rebase Vs Merge - RECAP

Merge

Very explicit, accurate history Visually complex Easy to roll back

Rebase

Linear history Easy to understand Not historically accurate Hard to undo.

Page 54: Everything you ever wanted to know about Git (But were afraid to ask)

Playing well with others

Page 55: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning

DevelopReleaseMaster

DevelopReleaseMaster

*/Customers-R/activation

* = http://github.paypal.com

*/lmarkus/activation

DevelopReleaseMaster

git remote add

<name> <url>

git clone

<url> originupstream

Page 56: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning

DevelopReleaseMaster

lmarkus/activation

DevelopReleaseMaster

originDevelopReleaseMaster

Customers-R/activation

upstream

person1/activation

person2/activation

person3/activation

Page 57: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning

UPSTREAM

ORIGIN

LOCAL

git remote updategit rebase upstream/developgit push origin develop

DEVELOP BRANCH

Page 58: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning - Common Error #1

UPSTREAM

ORIGIN

LOCAL

git push origin developDEVELOP BRANCH

git remote updategit rebase upstream/developgit push origin develop

Page 59: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning - Common Error #1

$ git push origin developTo https://github.paypal.com/lmarkus/activation.git ! [rejected] develop -> develop (non-fast-forward)error: failed to push some refs to https://github.paypal.com/lmarkus/activation.git

hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. Integrate the remote changes (e.g.hint: 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Page 60: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning - Common Error #1

UPSTREAM

ORIGIN

LOCAL

DEVELOP BRANCHOption 1:git pull origin developgit push origin master

Solves the problem, but history gets messy. Doubled up commits

Page 61: Everything you ever wanted to know about Git (But were afraid to ask)

Forking and Cloning - Common Error #1

UPSTREAM

ORIGIN

LOCAL

DEVELOP BRANCHOption 2:git push origin master --force

The Force has a dark side…You are overwriting history which may be shared

Page 62: Everything you ever wanted to know about Git (But were afraid to ask)

Resolving merge conflicts

Page 63: Everything you ever wanted to know about Git (But were afraid to ask)

What happens during a merge

Animals.txtAnimals I like:Dogs!

Animals.txtAnimals I like:Cats!Birds!

master feature1

git merge feature1

Animals.txtAnimals I like:<<<<<<< HEADDogs!=======Cats!Birds!>>>>>>> feature1

Page 64: Everything you ever wanted to know about Git (But were afraid to ask)

What happens during a merge

Animals.txtAnimals I like:Dogs!

Animals.txtAnimals I like:Cats!Birds!

master feature1

Animals.txtAnimals I like:<<<<<<< HEADDogs!=======Cats!Birds!>>>>>>> feature1

git add Animals.txtgit commit -m “Dogs Rule”

Page 65: Everything you ever wanted to know about Git (But were afraid to ask)

Resolve conflicts the smart way!

Page 66: Everything you ever wanted to know about Git (But were afraid to ask)

Resolve conflicts the smart way!

Shortcuts!

Page 67: Everything you ever wanted to know about Git (But were afraid to ask)

Resolve conflicts the smart way!

Page 68: Everything you ever wanted to know about Git (But were afraid to ask)