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

Post on 13-Apr-2017

1,702 views 1 download

transcript

Everything you 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

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

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

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!

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" "\\\$ "'

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

Git isn’t magic.

BLOBS TREES COMMITS

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

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

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

Trees organize (and name) sets of files

E9650Hello

a69b2

a.txt 557dbHello World

8984f

a.txt

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

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)

The world is my stage!

The world is my stage!

HEAD

STAGE

Work Dir

The world is my stage!

HEAD

STAGE

Work Dir

$ git statusOn branch masternothing to commit, working directory clean

The world is my stage!

HEAD

STAGE

Work Dir

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")

The world is my stage!

HEAD

STAGE

Work Dir

$ git add miner.txt

The world is my stage!

HEAD

STAGE

Work Dir

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

The world is my stage!

HEAD

STAGE

Work Dir

The world is my stage!

HEAD

STAGE

Work Dir

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

The world is my stage!

HEAD

STAGE

Work Dir

$ git commit -m “Mining is hard”

The world is my stage!

HEAD

STAGE

Work Dir

“Oops…” moments

Git Reset

A B C D E

STAGE WORKING DIR

E E

HEAD

Git Reset --soft

A B C D E

STAGE WORKING DIR

E E

HEAD

git reset --soft HEAD~1

Git Reset --mixed

A B C D E

STAGE WORKING DIR

D E

HEAD

git reset --mixed HEAD~1

E

Git Reset --hard

A B C D E

STAGE WORKING DIR

D D

HEAD

git reset --hard HEAD~1

E E

Clean up after yourself!

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

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

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

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

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

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.

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.

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

Branching out

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.

Branches

A B C

MASTER

DEVELOP

git checkout -b develop

Branches

A B C

MASTER

D E

DEVELOP

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

Branches

A B C

MASTER

D E

DEVELOP

git checkout master

Branches

A B C

MASTER

D E

DEVELOP

git commit -m “F”

F

Rebase Vs Merge

A B C

MASTER

D E

DEVELOP

F

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

Rebase

A B C

D E

DEVELOP

MASTER

FWe just re-wrote history!

git rebase develop

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.

Playing well with others

Forking and Cloning

DevelopReleaseMaster

DevelopReleaseMaster

*/Customers-R/activation

* = http://github.paypal.com

*/lmarkus/activation

DevelopReleaseMaster

git remote add

<name> <url>

git clone

<url> originupstream

Forking and Cloning

DevelopReleaseMaster

lmarkus/activation

DevelopReleaseMaster

originDevelopReleaseMaster

Customers-R/activation

upstream

person1/activation

person2/activation

person3/activation

Forking and Cloning

UPSTREAM

ORIGIN

LOCAL

git remote updategit rebase upstream/developgit push origin develop

DEVELOP BRANCH

Forking and Cloning - Common Error #1

UPSTREAM

ORIGIN

LOCAL

git push origin developDEVELOP BRANCH

git remote updategit rebase upstream/developgit push origin develop

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.

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

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

Resolving merge conflicts

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

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”

Resolve conflicts the smart way!

Resolve conflicts the smart way!

Shortcuts!

Resolve conflicts the smart way!