106
Understanding git Avik Das Vida Engineering

Understanding git

Embed Size (px)

Citation preview

Page 1: Understanding git

Understanding git

Avik DasVida Engineering

Page 2: Understanding git

A quick refresher on graphsBackground:

Page 3: Understanding git
Page 4: Understanding git

A

B

D C

Page 5: Understanding git

A

B

D C

Nodes + Edges = Graph

Page 6: Understanding git

A

B

D C

Nodes + Edges = Graph

Page 7: Understanding git

A

B

D C

Nodes + Edges = Graph

Page 8: Understanding git

A

B

D C

Nodes + Edges = Graph

Page 9: Understanding git

A

B

D C

Page 10: Understanding git

A

B

D C

Page 11: Understanding git

A

B

D C

Directed Edges → Directed Graph

Page 12: Understanding git

A

B

D C

Directed Edges → Directed Graph

Page 13: Understanding git

A

B

D C

Page 14: Understanding git

A

B

D C

cycle

Page 15: Understanding git

A

B

D C

Page 16: Understanding git

A

B

D C

Page 17: Understanding git

A

B

D C

No cycles → Directed Acyclic Graph

Page 18: Understanding git

A

B

D C

No cycles → Directed Acyclic Graph

Page 19: Understanding git

A

B

D C

DAG

Page 20: Understanding git

ABD C

Page 21: Understanding git

ABD C

DAG → Linearizable

Page 22: Understanding git

Git objects

Page 23: Understanding git

BLOB

Page 24: Understanding git

BLOB

def factorial(x):if x == 0:return 1else:return x * factorial(x - 1)print factorial(6)

Page 25: Understanding git

BLOB

def factorial(x):if x == 0:return 1else:return x * factorial(x - 1)print factorial(6)

066681188100ef24b1ce91a3a3596baffb620051

Page 26: Understanding git

BLOB

def factorial(x):if x == 0:return 1else:return x * factorial(x - 1)print factorial(6)

BLOB

def factorial(x): if x == 0: return 1 else: return x * factorial(x - 1)print factorial(6)

066681188100ef24b1ce91a3a3596baffb620051 59d8c4d1b5e8561375aa819adc513529b7ea19b8

Page 27: Understanding git

TREE

Page 28: Understanding git

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

Page 29: Understanding git

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

Page 30: Understanding git

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

BLOB

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi efficitur ex ex, ac lobortis velit scelerisque

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

ac72efa71e90424bdb41dc795609d572ddd88643

Page 31: Understanding git

COMMIT

Page 32: Understanding git

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

Page 33: Understanding git

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

Page 34: Understanding git

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

Page 35: Understanding git

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

COMMIT

tree ac72e parent 84ce1 author Avik Dascommitter Avik Das

Commit message

TREE

blob 5b1d3 READMEtree 03e78 libtree cdc8b testblob cba0a test.pyblob 911e7 xdiff

3f34c01a210400daf86c479b243fe50078c279c1

Page 36: Understanding git

Working directory → staging → repository

Page 37: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY COMMANDS

origin/master

Page 38: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V1)

COMMANDS

vim README

origin/master

Page 39: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V1)

COMMANDS

vim READMEgit add README

origin/master

Page 40: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V1)

COMMANDS

vim READMEgit add READMEvim README

README (V2)

origin/master

Page 41: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V2)

COMMANDS

vim READMEgit add READMEvim READMEgit add README

origin/master

Page 42: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V2)

COMMANDS

vim READMEgit add READMEvim READMEgit add READMEvim READMEREADME (V3)

origin/master

Page 43: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V2)

COMMANDS

git diff # between V2 and V3

README (V3)

origin/master

Page 44: Understanding git

C3C1 C2

HEAD

master

STAGING/INDEXWORKING DIRECTORY

README (V2)

COMMANDS

git diff # between V2 and V3git diff --staged # between V2 # and empty

README (V3)

origin/master

Page 45: Understanding git

C3C1 C2

HEAD

STAGING/INDEXWORKING DIRECTORY COMMANDS

git commit

README (V3)

C4

masterorigin/master

Page 46: Understanding git

C3C1 C2

HEAD

STAGING/INDEXWORKING DIRECTORY COMMANDS

git commitgit push# or git push origin master

README (V3)

C4

masterorigin/master

Page 47: Understanding git

Branching

Page 48: Understanding git

A B

git clone <url>

origin/master

master

HEAD

Page 49: Understanding git

A B

git clone <url>git pull

origin/master

master

HEAD

C

Page 50: Understanding git

A B

git clone <url>git pullgit checkout -b mine

origin/master

master

HEAD

C

mine

Page 51: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commit

origin/master

master

HEAD

C

mine

X

Page 52: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commit

origin/master

master

HEAD

C

mine

X Y

Page 53: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout master

origin/master

master

C

mine

X Y

HEAD

Page 54: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

origin/mastermaster

C

mine

X Y

HEAD

D E

Page 55: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout mine

origin/mastermaster

C

mine

X Y

D E

HEAD

Page 56: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout minegit merge master

origin/mastermaster

C

X Y

D E

M

mine HEAD

Page 57: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout minegit merge mastergit commit

origin/mastermaster

C

X Y

D E

M Z

mine HEAD

Page 58: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout mine

origin/mastermaster

C

mine

X Y

D E

HEAD

Page 59: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout minegit rebase master

origin/mastermaster

C

X Y

D E

X’

mine

Y’

HEAD

Page 60: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout minegit rebase mastergit commit

origin/mastermaster

C

X Y

D E

X’ Y’

mine

Z

HEAD

Page 61: Understanding git

A B

git clone <url>git pullgit checkout -b minegit commitgit commitgit checkout mastergit pull

git checkout minegit rebase mastergit commit

origin/mastermaster

C

X Y

D E

X’ Y’

mine

Z

HEADOnly reachable by SHA1

Page 62: Understanding git

Rewriting history

Page 63: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Page 64: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Main changes, split up over two WIP commits

Page 65: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Main changes, split up over two WIP commits

Needs some minor touch ups

Page 66: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Main changes, split up over two WIP commits

Needs some minor touch ups

Not actually

needed anymore

Page 67: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Main changes, split up over two WIP commits

Needs some minor touch ups

Commit message needs work

Not actually

needed anymore

Page 68: Understanding git

A B

master

C1 C2

git rebase -i master

C3 C4 C5

mine

Main changes, split up over two WIP commits

Needs some minor touch ups

Commit message needs work

Not actually

needed anymore

Page 69: Understanding git

pick 8af53d3 main feature WIPpick 76d8150 Refactorpick dd69f0c main feature WIP 2pick 974b13e On second thought, not neededpick 508fe0d Build on top of refactor

Page 70: Understanding git

pick 8af53d3 main feature WIPpick 76d8150 Refactorpick dd69f0c main feature WIP 2pick 974b13e On second thought, not neededpick 508fe0d Build on top of refactor

Page 71: Understanding git

pick 8af53d3 main feature WIPpick 76d8150 Refactorpick dd69f0c main feature WIP 2pick 508fe0d Build on top of refactor

Page 72: Understanding git

pick 8af53d3 main feature WIPpick 76d8150 Refactorpick dd69f0c main feature WIP 2pick 508fe0d Build on top of refactor

Page 73: Understanding git

pick 8af53d3 main feature WIPpick dd69f0c main feature WIP 2pick 76d8150 Refactorpick 508fe0d Build on top of refactor

Page 74: Understanding git

pick 8af53d3 main feature WIPsquash dd69f0c main feature WIP 2pick 76d8150 Refactorpick 508fe0d Build on top of refactor

Page 75: Understanding git

pick 8af53d3 main feature WIPsquash dd69f0c main feature WIP 2edit 76d8150 Refactorpick 508fe0d Build on top of refactor

Page 76: Understanding git

pick 8af53d3 main feature WIPsquash dd69f0c main feature WIP 2edit 76d8150 Refactorreword 508fe0d Build on top of refactor

Page 77: Understanding git

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

main feature WIP

# This is the 2nd commit's message:

main feature WIP

# Please enter the commit message for your changes.

SQUASH

Page 78: Understanding git

Implement main feature

Usual explanation

# Please enter the commit message for your changes.

SQUASH

Page 79: Understanding git

Stopped at 56b3bb2562e9ee1a0a984b45e7f2220f1148d189... To be editedYou can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

EDIT

Page 80: Understanding git

Build on top of refactor

# Please enter the commit message for your changes.

REWORD

Page 81: Understanding git

Build on top of refactor

Add some clarifying explanation.

# Please enter the commit message for your changes.

REWORD

Page 82: Understanding git

A B

master

C1 C2 C3 C4 C5

mine

Page 83: Understanding git

A B

master

C1 C2 C3 C4 C5

X C2’ C5’

mine

Page 84: Understanding git

A B

master

C1 C2 C3 C4 C5

X C2’ C5’

mine

Combination of commits C1 + C3

Page 85: Understanding git

A B

master

C1 C2 C3 C4 C5

X C2’ C5’

mine

Combination of commits C1 + C3Edited from

C2

Page 86: Understanding git

A B

master

C1 C2 C3 C4 C5

X C2’ C5’

mine

Combination of commits C1 + C3Edited from

C2

Reworded from C5

Page 87: Understanding git

A B

master

C1 C2

git push --force

C3 C4 C5

X C2’ C5’

mine

Combination of commits C1 + C3Edited from

C2

Reworded from C5

Page 88: Understanding git

git-rev-parse

Page 89: Understanding git

git rev-parse HEAD

Page 90: Understanding git

git rev-parse HEADgit rev-parse HEAD^

Page 91: Understanding git

git rev-parse HEADgit rev-parse HEAD^git rev-parse HEAD^^

Page 92: Understanding git

git rev-parse HEADgit rev-parse HEAD^git rev-parse HEAD^^git rev-parse HEAD^^^

Page 93: Understanding git

git rev-parse HEAD^^^

Page 94: Understanding git

git rev-parse HEAD~3

Page 95: Understanding git

git rev-parse master

Page 96: Understanding git

git rev-parse mastergit rev-parse master^

Page 97: Understanding git

git rev-parse 757e4cf

Page 98: Understanding git

Other useful commands

Page 99: Understanding git

git showgit show <ref>

Page 100: Understanding git

git resetgit reset --hardgit reset --soft

Page 101: Understanding git

git add -p

Page 102: Understanding git

git log <ref>

Page 103: Understanding git

git reflog

Page 104: Understanding git

git config

Page 105: Understanding git

git <verb> --helpgit help <verb>man git-<verb>

Page 106: Understanding git

Questions?