Advanced Git: Functionality and Features

263
1 © 2016 Brent Laster 1 © 2016 Brent Laster @BrentCLaster Advanced Git: Functionality and Features Open Source 101 Conference February 4, 2017 Brent Laster (author of Professional Git)

Transcript of Advanced Git: Functionality and Features

Page 1: Advanced Git: Functionality and Features

1

© 2016 Brent Laster

1

© 2016 Brent Laster@BrentCLaster

Advanced Git:Functionality and Features

Open Source 101 ConferenceFebruary 4, 2017

Brent Laster (author of Professional Git)

Page 2: Advanced Git: Functionality and Features

2

© 2016 Brent Laster

2

@BrentCLaster

About me

Senior Manager, R&D at SAS in Cary, NC Global Trainer and Speaker Git, Gerrit, Gradle, Jenkins, Pipelines Author - NFJS magazine, Professional Git book LinkedIn https://www.linkedin.com/in/brentlaster Twitter @BrentCLaster

Page 3: Advanced Git: Functionality and Features

3

© 2016 Brent Laster

3

@BrentCLaster

Professional Git

Available on: Amazon.com Wiley.com

Page 4: Advanced Git: Functionality and Features

4

© 2016 Brent Laster

4

@BrentCLaster

Agenda Core concepts refresh Merging and Rebasing Stash Reset and Revert Rerere Bisect Worktrees Submodules Subtrees Interactive Rebase Notes Grep

Page 5: Advanced Git: Functionality and Features

5

© 2016 Brent Laster

Check Out

CommitChekout

Checkout

Commit

Clone

Remote Repository

Local Environment

Central Server

Local Machine

Centralized Version Control Model Distributed Version Control Model

Centralized vs. Distributed VCS

Page 6: Advanced Git: Functionality and Features

6

© 2016 Brent Laster

6

@BrentCLaster

Git in One Picture

Dev

Prod

Public

Test

Page 7: Advanced Git: Functionality and Features

7

© 2016 Brent Laster

7

@BrentCLaster

Git in One Picture

Working DirectoryDev

Prod

Public

Test

Page 8: Advanced Git: Functionality and Features

8

© 2016 Brent Laster

8

@BrentCLaster

Git in One Picture

Staging Area

Working DirectoryDev

Prod

Public

Test

Page 9: Advanced Git: Functionality and Features

9

© 2016 Brent Laster

9

@BrentCLaster

Git in One Picture

Staging Area

Local Repository

Working DirectoryDev

Prod

Public

Test

Page 10: Advanced Git: Functionality and Features

10

© 2016 Brent Laster

10

@BrentCLaster

Git in One Picture

Staging Area

Local Repository

Working DirectoryDev

Prod

Public

Test

Page 11: Advanced Git: Functionality and Features

11

© 2016 Brent Laster

11

@BrentCLaster

Git in One Picture

Staging Area

Local Repository

Working DirectoryDev

Prod

Public

Test

Local Machine

Page 12: Advanced Git: Functionality and Features

12

© 2016 Brent Laster

12

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Local Machine

Page 13: Advanced Git: Functionality and Features

13

© 2016 Brent Laster

13

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Local Machine

Page 14: Advanced Git: Functionality and Features

14

© 2016 Brent Laster

14

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Local Machine

Page 15: Advanced Git: Functionality and Features

15

© 2016 Brent Laster

15

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Local Machine

Page 16: Advanced Git: Functionality and Features

16

© 2016 Brent Laster

16

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Add

Local Machine

Page 17: Advanced Git: Functionality and Features

17

© 2016 Brent Laster

17

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Add

Local Machine

Page 18: Advanced Git: Functionality and Features

18

© 2016 Brent Laster

18

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Add

Commit

Local Machine

Page 19: Advanced Git: Functionality and Features

19

© 2016 Brent Laster

19

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Server

Add

Commit

Local Machine

Page 20: Advanced Git: Functionality and Features

20

© 2016 Brent Laster

20

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Local Machine

Page 21: Advanced Git: Functionality and Features

21

© 2016 Brent Laster

21

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Local Machine

Page 22: Advanced Git: Functionality and Features

22

© 2016 Brent Laster

22

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push

Local Machine

Page 23: Advanced Git: Functionality and Features

23

© 2016 Brent Laster

23

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push

Local Machine

Page 24: Advanced Git: Functionality and Features

24

© 2016 Brent Laster

24

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push Clone

Local Machine

Page 25: Advanced Git: Functionality and Features

25

© 2016 Brent Laster

25

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push Clone

Local Machine

Page 26: Advanced Git: Functionality and Features

26

© 2016 Brent Laster

26

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push Clone

Local Machine

Fetch

Page 27: Advanced Git: Functionality and Features

27

© 2016 Brent Laster

27

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working DirectoryDev

Prod

Public

Test

Checkout

Server

Add

Commit

Push Clone

Local Machine

Fetch

Page 28: Advanced Git: Functionality and Features

28

© 2016 Brent Laster

28

@BrentCLaster

Git in One Picture

Staging Area

Remote Repository

Local Repository

Working Directory

Pull

Dev

Prod

Public

Test

Checkout

Server

Add

Commit

Push Clone

Local Machine

Fetch

Page 29: Advanced Git: Functionality and Features

29

© 2016 Brent Laster

29

@BrentCLaster

Git Granularity (What is a unit?)

Page 30: Advanced Git: Functionality and Features

30

© 2016 Brent Laster

30

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

Page 31: Advanced Git: Functionality and Features

31

© 2016 Brent Laster

31

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

file1.javafile1.java

Page 32: Advanced Git: Functionality and Features

32

© 2016 Brent Laster

32

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

file1.java

CVS

file1.java

Page 33: Advanced Git: Functionality and Features

33

© 2016 Brent Laster

33

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

file1.java

CVS

file1.javaDelta

Page 34: Advanced Git: Functionality and Features

34

© 2016 Brent Laster

34

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

file1.java

CVS

file1.javaDelta

Page 35: Advanced Git: Functionality and Features

35

© 2016 Brent Laster

35

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

dir: proj1

file1.java

file2.java

Working directory

file1.java

CVS

file1.javaDelta

Page 36: Advanced Git: Functionality and Features

36

© 2016 Brent Laster

36

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

dir: proj1

file1.java

file2.java

Working directory

file1.java

CVS

file1.javaDelta

Page 37: Advanced Git: Functionality and Features

37

© 2016 Brent Laster

37

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

dir: proj1

file1.java

file2.java

Working directory

file1.java

CVS

Git Commit

file1.javaDelta

Page 38: Advanced Git: Functionality and Features

38

© 2016 Brent Laster

38

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

dir: proj1

file1.java

file2.java

Working directory

file1.java

CVS

Git Commit

file1.javaDelta

Page 39: Advanced Git: Functionality and Features

39

© 2016 Brent Laster

39

@BrentCLaster

Git Granularity (What is a unit?)

In traditional source control, the unit of granularity is usually a file

In Git, the unit of granularity is usually a tree

dir: proj1

file1.java

file2.java

Working directory

file1.java

CVS

Git

file1.javaDelta

Commit

Snapshot

Page 40: Advanced Git: Functionality and Features

40

© 2016 Brent Laster

40

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)

C2

C3

C0 C1 C4

feature

hotfix

Page 41: Advanced Git: Functionality and Features

41

© 2016 Brent Laster

41

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)$ git checkout master

C2

C3

C0 C1 C4

feature

hotfix

Page 42: Advanced Git: Functionality and Features

42

© 2016 Brent Laster

42

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)$ git checkout master$ git merge hotfix

C2

C3

C0 C1 C4

feature

hotfix

Page 43: Advanced Git: Functionality and Features

43

© 2016 Brent Laster

43

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)$ git checkout master$ git merge hotfixUpdating f42c576..3a0874cFast ForwardREADME | 1-1 files changed, 0 insertions(+) 1 deletions (-)

C2

C3

C0 C1 C4

feature

hotfix

Page 44: Advanced Git: Functionality and Features

44

© 2016 Brent Laster

44

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)$ git checkout master$ git merge hotfixUpdating f42c576..3a0874cFast ForwardREADME | 1-1 files changed, 0 insertions(+) 1 deletions (-)

About “Fast Forward” – because commit pointed to by branch merged was directly “upstream” of the current commit, Git moves the pointer forward

C2

C3

C0 C1 C4

feature

hotfix

Page 45: Advanced Git: Functionality and Features

45

© 2016 Brent Laster

45

@BrentCLaster

Merging: What is a Fast-forward?

Assume you have three branches as below You want to merge hotfix into master (so master will

have your hotfix for future development)$ git checkout master$ git merge hotfixUpdating f42c576..3a0874cFast ForwardREADME | 1-1 files changed, 0 insertions(+) 1 deletions (-)

About “Fast Forward” – because commit pointed to by branch merged was directly “upstream” of the current commit, Git moves the pointer forward

(Both branches were in the same line of development, so the net result is that master and hotfix point to the same commit)

C2

C3

C0 C1 C4

feature

hotfix

Page 46: Advanced Git: Functionality and Features

46

© 2016 Brent Laster

46

@BrentCLaster

Assume branching scenario below

Merging: What is a 3-way Merge?

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 47: Advanced Git: Functionality and Features

47

© 2016 Brent Laster

47

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot)

Merging: What is a 3-way Merge?

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 48: Advanced Git: Functionality and Features

48

© 2016 Brent Laster

48

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature

Merging: What is a 3-way Merge?

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 49: Advanced Git: Functionality and Features

49

© 2016 Brent Laster

49

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5)

Merging: What is a 3-way Merge?

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 50: Advanced Git: Functionality and Features

50

© 2016 Brent Laster

50

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 51: Advanced Git: Functionality and Features

51

© 2016 Brent Laster

51

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

$ git checkout master

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 52: Advanced Git: Functionality and Features

52

© 2016 Brent Laster

52

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

$ git checkout master$ git merge feature

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 53: Advanced Git: Functionality and Features

53

© 2016 Brent Laster

53

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

$ git checkout master$ git merge feature

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 54: Advanced Git: Functionality and Features

54

© 2016 Brent Laster

54

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

$ git checkout master$ git merge feature

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 55: Advanced Git: Functionality and Features

55

© 2016 Brent Laster

55

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor

Merging: What is a 3-way Merge?

$ git checkout master$ git merge feature

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 56: Advanced Git: Functionality and Features

56

© 2016 Brent Laster

56

@BrentCLaster

Assume branching scenario below master and feature branches have both diverged (changed) since their last

common ancestor (commit/snapshot) Intent is to change to master and merge in feature Current commit on target branch isn’t a direct ancestor of current commit on

branch you’re merging in (i.e. C4 isn’t on the same line of development as C5) Git does 3-way merge using common ancestor Instead of just moving branch pointer forward, Git creates a new snapshot and

a new commit that points to it called a “merge commit”

Merging: What is a 3-way Merge?

$ git checkout master$ git merge feature

C2

C3

C4

C5

feature

C0 C1

Common Ancestor

Branch Tip

Branch Tip

master

Page 57: Advanced Git: Functionality and Features

57

© 2016 Brent Laster

57

@BrentCLaster

Merging: What is a Rebase?

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 58: Advanced Git: Functionality and Features

58

© 2016 Brent Laster

58

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one.

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 59: Advanced Git: Functionality and Features

59

© 2016 Brent Laster

59

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 60: Advanced Git: Functionality and Features

60

© 2016 Brent Laster

60

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base)

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 61: Advanced Git: Functionality and Features

61

© 2016 Brent Laster

61

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 62: Advanced Git: Functionality and Features

62

© 2016 Brent Laster

62

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 63: Advanced Git: Functionality and Features

63

© 2016 Brent Laster

63

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 64: Advanced Git: Functionality and Features

64

© 2016 Brent Laster

64

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process:

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 65: Advanced Git: Functionality and Features

65

© 2016 Brent Laster

65

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process:

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1

master

Page 66: Advanced Git: Functionality and Features

66

© 2016 Brent Laster

66

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process: Goes to the common ancestor of the two branches (the one you are on and the one you are rebasing

onto)

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1 C2

master

Page 67: Advanced Git: Functionality and Features

67

© 2016 Brent Laster

67

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process: Goes to the common ancestor of the two branches (the one you are on and the one you are rebasing

onto) Gets the diff introduced by each commit of the branch you are on, saving them to temporary files

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1 C2

master

Page 68: Advanced Git: Functionality and Features

68

© 2016 Brent Laster

68

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process: Goes to the common ancestor of the two branches (the one you are on and the one you are rebasing

onto) Gets the diff introduced by each commit of the branch you are on, saving them to temporary files Applies each change in turn

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1 C3’ C5’C2

master

Page 69: Advanced Git: Functionality and Features

69

© 2016 Brent Laster

69

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process: Goes to the common ancestor of the two branches (the one you are on and the one you are rebasing

onto) Gets the diff introduced by each commit of the branch you are on, saving them to temporary files Applies each change in turn Moves the branch to the new rebase point

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1 C3’ C5’C2

master

Page 70: Advanced Git: Functionality and Features

70

© 2016 Brent Laster

70

@BrentCLaster

Merging: What is a Rebase? Rebase – take all of the changes that were committed on one branch and replay

them on another one. Concepts in simple syntax (git rebase branch2 [branch1]):

Move branchpoint of branch (carrying along all commits) to be off of a different commit (new base) Current branch (or branch1 if supplied) is one having its branchpoint moved Branch2 (or commit) provided as first/only argument is the new branchpoint In simple case, think of it as “pick up branch1 entirely and move its branchpoint to be after the tip of

branch2)”

Process: Goes to the common ancestor of the two branches (the one you are on and the one you are rebasing

onto) Gets the diff introduced by each commit of the branch you are on, saving them to temporary files Applies each change in turn Moves the branch to the new rebase point

$ git checkout feature$ git rebase master

C3

C4

C5

C0

Common Ancestor

C2

feature

C1 C3’ C5’C2

master

Page 71: Advanced Git: Functionality and Features

71

© 2016 Brent Laster

71

@BrentCLaster

Command: Git Stash Purpose -- allow you to keep a backup copy of your work

that hasn’t been committed yet Use case - you want to switch branches but don’t want

to lose work that hasn’t been committed; you want to save something you’ve tried and may want to come back to

Syntax:

Page 72: Advanced Git: Functionality and Features

72

© 2016 Brent Laster

72

@BrentCLaster

Stash

Page 73: Advanced Git: Functionality and Features

73

© 2016 Brent Laster

73

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

Page 74: Advanced Git: Functionality and Features

74

© 2016 Brent Laster

74

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory*

Page 75: Advanced Git: Functionality and Features

75

© 2016 Brent Laster

75

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory*

*

Page 76: Advanced Git: Functionality and Features

76

© 2016 Brent Laster

76

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory*

*

> git stash

Page 77: Advanced Git: Functionality and Features

77

© 2016 Brent Laster

77

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory*

*

> git stash

Page 78: Advanced Git: Functionality and Features

78

© 2016 Brent Laster

78

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{0}

> git stash

Page 79: Advanced Git: Functionality and Features

79

© 2016 Brent Laster

79

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

**

stash@{0}

> git stash

Page 80: Advanced Git: Functionality and Features

80

© 2016 Brent Laster

80

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

**

**

stash@{0}

> git stash

Page 81: Advanced Git: Functionality and Features

81

© 2016 Brent Laster

81

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

**

**

stash@{0}

> git stash

#

Page 82: Advanced Git: Functionality and Features

82

© 2016 Brent Laster

82

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

**

**

stash@{0}

> git stash > git stash -u

#

Page 83: Advanced Git: Functionality and Features

83

© 2016 Brent Laster

83

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}

stash@{0}

stash@{1}

> git stash -u

Page 84: Advanced Git: Functionality and Features

84

© 2016 Brent Laster

84

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

***

stash@{0}

stash@{0}

stash@{1}

> git stash -u

Page 85: Advanced Git: Functionality and Features

85

© 2016 Brent Laster

85

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

***

***

stash@{0}

stash@{0}

stash@{1}

> git stash -u

Page 86: Advanced Git: Functionality and Features

86

© 2016 Brent Laster

86

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

***

***

stash@{0}

stash@{0}

stash@{1}

> git stash save “cool feature”

Page 87: Advanced Git: Functionality and Features

87

© 2016 Brent Laster

87

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}Staging Area ***

stash@{2}

stash@{1}

stash@{0}

stash@{0}stash@{1}

stash@{1}stash@{2}

“cool feature”

Working Directory

***

Page 88: Advanced Git: Functionality and Features

88

© 2016 Brent Laster

88

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}Staging Area ***

stash@{2}

stash@{1}

stash@{0}

stash@{0}stash@{1}

stash@{1}stash@{2}

“cool feature”

Working Directory

***

> git stash apply stash@{1}

Page 89: Advanced Git: Functionality and Features

89

© 2016 Brent Laster

89

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}Staging Area ***

stash@{2}

**

**

stash@{1}

stash@{0}

stash@{0}stash@{1}

stash@{1}stash@{2}

“cool feature”

Working Directory

*** #

> git stash apply stash@{1}

Page 90: Advanced Git: Functionality and Features

90

© 2016 Brent Laster

90

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

stash@{0}

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}Staging Area ***

stash@{2}

**

**

stash@{1}

stash@{0}

stash@{0}stash@{1}

stash@{1}stash@{2}

“cool feature”

Working Directory

*** #

$ git stash pop stash@{2}

Page 91: Advanced Git: Functionality and Features

91

© 2016 Brent Laster

91

@BrentCLaster

Stash

Staging AreaStaging Area

Local Repository

Working Directory

Staging Area *

Working Directory *

stash@{1}Staging Area **

Working Directory

**#

stash@{0}Staging Area ***

***

***

stash@{0}stash@{1}

“cool feature”

Working Directory

*** #

$ git stash pop stash@{2}

Page 92: Advanced Git: Functionality and Features

92

© 2016 Brent Laster

92

@BrentCLaster

Command: Git Reset Purpose -- allow you to “roll back” so that your branch

points at a previous commit ; optionally also update staging area and working directory to that commit

Use case - you want to update your local environment back to a previous point in time; you want to overwrite or a local change you’ve made

Syntax:

Warning: --hard overwrites everything

Page 93: Advanced Git: Functionality and Features

93

© 2016 Brent Laster

93

@BrentCLaster

Command: Git Revert Purpose -- allow you to “undo” by adding a new change

that cancels out effects of previous one Use case - you want to cancel out a previous change but

not roll things back Syntax:

Note: The net result of using this command vs. reset can be the same. If so, and content that is being reset/revert has been pushed such that others may be consuming it, preference is for revert.

Page 94: Advanced Git: Functionality and Features

94

© 2016 Brent Laster

94

@BrentCLaster

Local Repository

Reset and Revert

Line 1

Staging Area

Working Directory

XYZ

ABC

Line 1

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1Line 2Line 1Line 1 Line 1Line 2

tag: current

Page 95: Advanced Git: Functionality and Features

95

© 2016 Brent Laster

95

@BrentCLaster

Local Repository

git reset --hard 87ba8bc

Reset and Revert

Line 1

Staging Area

Working Directory

XYZ

ABC

Line 1

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1Line 2

Line 1

Line 1 Line 1Line 2

tag: current

Page 96: Advanced Git: Functionality and Features

96

© 2016 Brent Laster

96

@BrentCLaster

Local Repository

git reset --hard 87ba8bc

Reset and Revert

Line 1

Staging Area

Working Directory

XYZ

ABC

Line 1

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1Line 2

Line 1

Line 1 Line 1Line 2

tag: current

Page 97: Advanced Git: Functionality and Features

97

© 2016 Brent Laster

97

@BrentCLaster

Local Repository

git reset current~1 [--mixed]

Reset and Revert

Line 1

Staging Area

Working Directory

XYZ

ABC

Line 1

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1Line 2

Line 1

Line 1 Line 1Line 2

tag: current

Page 98: Advanced Git: Functionality and Features

98

© 2016 Brent Laster

98

@BrentCLaster

Local Repository

git reset current~1 [--mixed]

Reset and Revert

Line 1

Staging Area

Working Directory

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1 Line 1Line 2

tag: current

Page 99: Advanced Git: Functionality and Features

99

© 2016 Brent Laster

99

@BrentCLaster

Local Repository

git revert HEAD~1

Reset and Revert

Line 1

Staging Area

Working Directory

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

Line 1Line 2

Line 1 Line 1Line 2

tag: current

Page 100: Advanced Git: Functionality and Features

100

© 2016 Brent Laster

100

@BrentCLaster

Local Repository

git revert HEAD~1

Reset and Revert

Line 1

Staging Area

Working Directory

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

c1c8bd4

Line 1Line 2

Line 1 Line 1Line 2

tag: current

Page 101: Advanced Git: Functionality and Features

101

© 2016 Brent Laster

101

@BrentCLaster

Local Repository

Reset and Revert

Line 1

Staging Area

Working Directory

87ba8bc 43bd3ef d21be2c

Line 1Line 2Line 3

c1c8bd4

Line 1Line 2

Line 1 Line 1Line 2

tag: current

Page 102: Advanced Git: Functionality and Features

102

© 2016 Brent Laster

102

@BrentCLaster

Command: Git Rerere (Reuse Recorded Resolution) Purpose -- allows recording of how you solve a merge

situation and then can automatically resolve the same situation in the same way if needed later

Use case - trial and repeated merges; merging newer versions of a branch with the same conflicts into a newer one periodically; resolve conflicts after reset or revert; applicable to any repeated merge case: rebase, merge

Syntax: Note: This is a “state” command. Enabled by turning on

a state in Git, rather than just running a command Enabled via git config --global rerere.enabled 1

» Then runs automatically Invoked directly via git rerere for related commands or options

Page 103: Advanced Git: Functionality and Features

103

© 2016 Brent Laster

103

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

File C

File A

File B

master

File A

File C

File B

File A

File C

File BFile B

File A File C

Git Rerere 1

Page 104: Advanced Git: Functionality and Features

104

© 2016 Brent Laster

104

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master

File C

File A

File B

master

File A

File C

File B

File A

File C

File BFile B

File A File C

Git Rerere 1

Page 105: Advanced Git: Functionality and Features

105

© 2016 Brent Laster

105

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1

File C

File A

File B

master

File A

File C

File B

File A

File C

File BFile B

File A File C

rr-cacheGit Rerere 1

Page 106: Advanced Git: Functionality and Features

106

© 2016 Brent Laster

106

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature

File C

File A

File B

master

File A

File C

File B

File A

File C

X

File A File C

rr-cacheGit Rerere 1

Page 107: Advanced Git: Functionality and Features

107

© 2016 Brent Laster

107

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cacheGit Rerere 1

Page 108: Advanced Git: Functionality and Features

108

© 2016 Brent Laster

108

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cache

File A File C

Git Rerere 1

Page 109: Advanced Git: Functionality and Features

109

© 2016 Brent Laster

109

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 110: Advanced Git: Functionality and Features

110

© 2016 Brent Laster

110

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 111: Advanced Git: Functionality and Features

111

© 2016 Brent Laster

111

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 112: Advanced Git: Functionality and Features

112

© 2016 Brent Laster

112

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>File B

File A File CFile A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 113: Advanced Git: Functionality and Features

113

© 2016 Brent Laster

113

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add .

File C

File A

File B

master

File A

File C

File B

File A

File C

File B

File A File CFile BFile A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 114: Advanced Git: Functionality and Features

114

© 2016 Brent Laster

114

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add . $ git commit –m “finalize merge”

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimageGit Rerere 1

Page 115: Advanced Git: Functionality and Features

115

© 2016 Brent Laster

115

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add . $ git commit –m “finalize merge”

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimage postimageGit Rerere 1

Page 116: Advanced Git: Functionality and Features

116

© 2016 Brent Laster

116

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add . $ git commit –m “finalize merge”

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimage postimage

File BGit Rerere 1

Page 117: Advanced Git: Functionality and Features

117

© 2016 Brent Laster

117

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add . $ git commit –m “finalize merge” Recorded resolution for ‘File B’

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimage postimage

File BGit Rerere 1

Page 118: Advanced Git: Functionality and Features

118

© 2016 Brent Laster

118

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git checkout master$ git config --global rerere.enabled 1$ git merge feature Recorded preimage for ‘File B’$ git status

Changes to be committed:modified: File Amodified: File C

Unmerged pathsboth modified: File B

[fix conflicts]$ git add . $ git commit –m “finalize merge” Recorded resolution for ‘File B’

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimage postimage

File BGit Rerere 1

Page 119: Advanced Git: Functionality and Features

119

© 2016 Brent Laster

119

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git reset --hard HEAD~1

File C

File A

File B

master

File A

File C

File B

File A

File C

File BFile B

File A File C

rr-cacheFile B<<>>

preimage postimage

File BGit Rerere 2

Page 120: Advanced Git: Functionality and Features

120

© 2016 Brent Laster

120

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git reset --hard HEAD~1$ git checkout master

File C

File A

File B

master

File A

File C

File B

File A

File C

File BFile B

File A File C

rr-cacheFile B<<>>

preimage postimage

File BGit Rerere 2

Page 121: Advanced Git: Functionality and Features

121

© 2016 Brent Laster

121

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git reset --hard HEAD~1$ git checkout master$ git merge feature Merge conflict in ‘File B’

File C

File A

File B

master

File A

File C

File B

File A

File C

File B<<>>

File A File CFile A File C

rr-cache

File A File C

File B<<>>

preimage postimage

File BGit Rerere 2

Page 122: Advanced Git: Functionality and Features

122

© 2016 Brent Laster

122

© 2016 Brent Laster@BrentCLaster

Local Repository

feature

Staging Area

File B

Working Directory

$ git reset --hard HEAD~1$ git checkout master$ git merge feature Merge conflict in ‘File B’ Resolved ‘File B’ using previous resolution

File C

File A

File B

master

File A

File C

File B

File A

File C

File A File C

File B

File A File C

rr-cache

File A File C

File B<<>>

preimage postimage

File BGit Rerere 2

Page 123: Advanced Git: Functionality and Features

123

© 2016 Brent Laster

123

@BrentCLaster

Command: Bisect Purpose - Use “automated” binary search through Git’s history to find a

specific commit that first introduced a problem (i.e. “first bad commit”) Use case - Quickly locate the commit in Git’s history that introduced a

bug Syntax:

Page 124: Advanced Git: Functionality and Features

124

© 2016 Brent Laster

124

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 125: Advanced Git: Functionality and Features

125

© 2016 Brent Laster

125

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 126: Advanced Git: Functionality and Features

126

© 2016 Brent Laster

126

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 127: Advanced Git: Functionality and Features

127

© 2016 Brent Laster

127

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 128: Advanced Git: Functionality and Features

128

© 2016 Brent Laster

128

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 129: Advanced Git: Functionality and Features

129

© 2016 Brent Laster

129

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 130: Advanced Git: Functionality and Features

130

© 2016 Brent Laster

130

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code git bisect start

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 131: Advanced Git: Functionality and Features

131

© 2016 Brent Laster

131

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code git bisect start git bisect bad

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 132: Advanced Git: Functionality and Features

132

© 2016 Brent Laster

132

@BrentCLaster

LOCAL REPOSITORY

Version 1Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 133: Advanced Git: Functionality and Features

133

© 2016 Brent Laster

133

@BrentCLaster

LOCAL REPOSITORY

Version 1

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 134: Advanced Git: Functionality and Features

134

© 2016 Brent Laster

134

@BrentCLaster

LOCAL REPOSITORY

Version 1

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 135: Advanced Git: Functionality and Features

135

© 2016 Brent Laster

135

@BrentCLaster

LOCAL REPOSITORY

Version 1

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 136: Advanced Git: Functionality and Features

136

© 2016 Brent Laster

136

@BrentCLaster

LOCAL REPOSITORY

Version 1

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 137: Advanced Git: Functionality and Features

137

© 2016 Brent Laster

137

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 138: Advanced Git: Functionality and Features

138

© 2016 Brent Laster

138

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 139: Advanced Git: Functionality and Features

139

© 2016 Brent Laster

139

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 140: Advanced Git: Functionality and Features

140

© 2016 Brent Laster

140

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 5

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 141: Advanced Git: Functionality and Features

141

© 2016 Brent Laster

141

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 142: Advanced Git: Functionality and Features

142

© 2016 Brent Laster

142

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 143: Advanced Git: Functionality and Features

143

© 2016 Brent Laster

143

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 144: Advanced Git: Functionality and Features

144

© 2016 Brent Laster

144

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 7

Version 6

WORKING DIRECTORY

Page 145: Advanced Git: Functionality and Features

145

© 2016 Brent Laster

145

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

WORKING DIRECTORY

Page 146: Advanced Git: Functionality and Features

146

© 2016 Brent Laster

146

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

WORKING DIRECTORY

Page 147: Advanced Git: Functionality and Features

147

© 2016 Brent Laster

147

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

WORKING DIRECTORY

Page 148: Advanced Git: Functionality and Features

148

© 2016 Brent Laster

148

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6) try code

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

WORKING DIRECTORY

Page 149: Advanced Git: Functionality and Features

149

© 2016 Brent Laster

149

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6) try code git bisect bad (git reports version 6 as the first

bad commit)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

WORKING DIRECTORY

Page 150: Advanced Git: Functionality and Features

150

© 2016 Brent Laster

150

@BrentCLaster

LOCAL REPOSITORY

Version 1

Bisect

checkout latest version try code git bisect start git bisect bad checkout earlier version ( user checks out) try code git bisect good (bisect checks out version 5) try code git bisect good (bisect checks out version 7) try code git bisect bad (bisect checks out version 6) try code git bisect bad (git reports version 6 as the first

bad commit)

Version 3

Version 4

Version 5

Version 6

Version 7

Version 8

Version 9

Version 10

Version 2

Version 6

FIRST BAD COMMIT

WORKING DIRECTORY

Page 151: Advanced Git: Functionality and Features

151

© 2016 Brent Laster

151

@BrentCLaster

Background: Switching between Branches

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 152: Advanced Git: Functionality and Features

152

© 2016 Brent Laster

152

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 153: Advanced Git: Functionality and Features

153

© 2016 Brent Laster

153

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 154: Advanced Git: Functionality and Features

154

© 2016 Brent Laster

154

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 155: Advanced Git: Functionality and Features

155

© 2016 Brent Laster

155

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch>

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 156: Advanced Git: Functionality and Features

156

© 2016 Brent Laster

156

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch>

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 157: Advanced Git: Functionality and Features

157

© 2016 Brent Laster

157

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch> Updates indicators

Local Repository

Working Directory

87ba8bc

43bd3ef d21be2c c1c8bd4

Page 158: Advanced Git: Functionality and Features

158

© 2016 Brent Laster

158

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch> Updates indicators

git branch

Local Repository

Working Directory

*

87ba8bc

43bd3ef d21be2c c1c8bd4

mastertesting

Page 159: Advanced Git: Functionality and Features

159

© 2016 Brent Laster

159

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch> Updates indicators

git checkout testing

git branch

Local Repository

Working Directory

*

87ba8bc

43bd3ef d21be2c c1c8bd4

mastertesting

Page 160: Advanced Git: Functionality and Features

160

© 2016 Brent Laster

160

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch> Updates indicators

git checkout testinggit checkout master

git branch

Local Repository

Working Directory

*

87ba8bc

43bd3ef d21be2c c1c8bd4

mastertesting

Page 161: Advanced Git: Functionality and Features

161

© 2016 Brent Laster

161

@BrentCLaster

Background: Switching between BranchesCommand: git checkout <branch>git checkout master

Does three things Moves HEAD pointer back to <branch> Reverts files in working directory to

snapshot pointed to by <branch> Updates indicators

git checkout testinggit checkout mastergit checkout testing

git branch

Local Repository

Working Directory

*

87ba8bc

43bd3ef d21be2c c1c8bd4

mastertesting

Page 162: Advanced Git: Functionality and Features

162

© 2016 Brent Laster

162

@BrentCLaster

Command: Worktrees Purpose - Allows multiple, separate Working Areas attached

to one Local Repository Use case - Simultaneous development in multiple branche Syntax

git worktree add [-f] [--detach] [-b <new-branch>] <path> [<branch>] git worktree list [--porcelain] git worktree prune [-n] [-v] [--expire <expire>]

Notes “Traditional” working directory is called the main working tree; Any

new trees you create with this command are called linked working trees

Information about working trees is stored in the .git area (assuming .git default GIT_DIR is used)

Working tree information is stored in .git/worktrees/<name of worktree>.

Page 163: Advanced Git: Functionality and Features

163

© 2016 Brent Laster

163

@BrentCLaster

Worktrees

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

Page 164: Advanced Git: Functionality and Features

164

© 2016 Brent Laster

164

@BrentCLaster

Worktrees git worktree add -b exp tree1

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

Page 165: Advanced Git: Functionality and Features

165

© 2016 Brent Laster

165

@BrentCLaster

Worktrees git worktree add -b exp tree1

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

worktrees/ tree1

Page 166: Advanced Git: Functionality and Features

166

© 2016 Brent Laster

166

@BrentCLaster

Worktrees git worktree add -b exp tree1

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

worktrees/ tree1

Page 167: Advanced Git: Functionality and Features

167

© 2016 Brent Laster

167

@BrentCLaster

Worktrees git worktree add -b exp tree1

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

worktrees/ tree1

gitdirHEAD

etc.

Page 168: Advanced Git: Functionality and Features

168

© 2016 Brent Laster

168

@BrentCLaster

Worktrees git worktree add -b exp tree1

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

worktrees/ tree1

gitdirHEAD

etc.

tree1

Linked Working Tree

Staging Area

Working Directory (exp)

Page 169: Advanced Git: Functionality and Features

169

© 2016 Brent Laster

169

@BrentCLaster

Worktrees git worktree add -b exp tree1 git worktree add -b prod tree2

Remote Repository

Main Working Tree

Server

Add

Commit

Push Pull

Staging Area

Local Repository

Working Directory (master)

Local Machine

worktrees/ tree1

gitdirHEAD

etc.

tree1

Linked Working Tree

Staging Area

Working Directory (exp)

gitdirHEAD

etc.

tree2

Linked Working Tree

Staging Area

Working Directory (prod)

tree2

Page 170: Advanced Git: Functionality and Features

170

© 2016 Brent Laster

170

@BrentCLaster

Command: Submodules Purpose - Allows including a separate repository with your current repository Use case - include the Git repository for one or more dependencies along with the

original repository for a project Syntax

git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--depth <depth>] [--] <repository> [<path>]

git submodule [--quiet] status [--cached] [--recursive] [--] [<path>… ]git submodule [--quiet] init [--] [<path>… ]git submodule [--quiet] deinit [-f|--force] [--] <path>… git submodule [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase|--merge] [--reference <repository>]

[--depth <depth>] [--recursive] [--] [<path>… ]git submodule [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]

[commit] [--] [<path>… ]git submodule [--quiet] foreach [--recursive] <command>git submodule [--quiet] sync [--recursive] [--] [<path>… ]

Notes Creates a subdirectory off of your original repository that contains a clone of another Git

repository Original repository is typically called superproject Metadata stored in .gitmodules file

Page 171: Advanced Git: Functionality and Features

171

© 2016 Brent Laster

171

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Submodules 1

Page 172: Advanced Git: Functionality and Features

172

© 2016 Brent Laster

172

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

What happens when you add a submodule?

Submodules 1

Page 173: Advanced Git: Functionality and Features

173

© 2016 Brent Laster

173

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

What happens when you add a submodule?

Submodules 1

Page 174: Advanced Git: Functionality and Features

174

© 2016 Brent Laster

174

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

Submodules 1

Page 175: Advanced Git: Functionality and Features

175

© 2016 Brent Laster

175

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

Submodules 1

Page 176: Advanced Git: Functionality and Features

176

© 2016 Brent Laster

176

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.

Submodules 1

Page 177: Advanced Git: Functionality and Features

177

© 2016 Brent Laster

177

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.

Submodules 1

Page 178: Advanced Git: Functionality and Features

178

© 2016 Brent Laster

178

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

Submodules 1

Page 179: Advanced Git: Functionality and Features

179

© 2016 Brent Laster

179

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

.gitmodules

Submodules 1

Page 180: Advanced Git: Functionality and Features

180

© 2016 Brent Laster

180

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

.gitmodules

Submodules 1

Page 181: Advanced Git: Functionality and Features

181

© 2016 Brent Laster

181

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

.gitmodules

Submodules 1

Page 182: Advanced Git: Functionality and Features

182

© 2016 Brent Laster

182

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

.gitmodules

Submodules 1

Page 183: Advanced Git: Functionality and Features

183

© 2016 Brent Laster

183

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

.gitmodules

Submodules 1

Page 184: Advanced Git: Functionality and Features

184

© 2016 Brent Laster

184

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

.gitmodulesmod1

<current commit>

Submodules 1

Page 185: Advanced Git: Functionality and Features

185

© 2016 Brent Laster

185

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

.gitmodulesmod1

<current commit>

Submodules 1

Page 186: Advanced Git: Functionality and Features

186

© 2016 Brent Laster

186

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

.gitmodulesmod1

<current commit>

Submodules 1

Page 187: Advanced Git: Functionality and Features

187

© 2016 Brent Laster

187

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

Submodules 1

Page 188: Advanced Git: Functionality and Features

188

© 2016 Brent Laster

188

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

submodulereference

Submodules 1

Page 189: Advanced Git: Functionality and Features

189

© 2016 Brent Laster

189

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

submodulereference

commit pointed to by submodulereference

Submodules 1

Page 190: Advanced Git: Functionality and Features

190

© 2016 Brent Laster

190

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

submodulereference

commit pointed to by submodulereference

Head of branch

Submodules 1

Page 191: Advanced Git: Functionality and Features

191

© 2016 Brent Laster

191

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

submodulereference

commit pointed to by submodulereference

Head of branch

8. Push

Submodules 1

Page 192: Advanced Git: Functionality and Features

192

© 2016 Brent Laster

192

@BrentCLaster

Remote Repository

proj_dir

superproject

Staging Area

Local Repository

Working Directory

Remote Repository

proj_dir/mod1

submodule 1

Staging Area

Working Directory

Local Repository

What happens when you add a submodule? 1. Git clones down the repository for the submodule into the current directory.

2. By default, Git checks out the master branch.3. Git adds the submodule’s path for cloning to the .gitmodules file.

4. Git adds the .gitmodules file to the index, ready to be committed.

5. Git adds the current commit ID of the submodule to the index, ready to be committed.

6. To complete the add process, you need to complete the Git workflow for the staged changes. From the superproject’s directory:

7. Commit

modules/mod1

gitdirHEAD

etc.

submodulereference

commit pointed to by submodulereference

Head of branch

8. Push

Submodules 1

Page 193: Advanced Git: Functionality and Features

193

© 2016 Brent Laster

193

@BrentCLaster

Remote Repository

How do we clone a repository with submodules?

Submodules 2

Page 194: Advanced Git: Functionality and Features

194

© 2016 Brent Laster

194

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

modules/mod1

gitdirHEAD

etc.

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

Submodules 2

Page 195: Advanced Git: Functionality and Features

195

© 2016 Brent Laster

195

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

modules/mod1

gitdirHEAD

etc.

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

Submodules 2

Page 196: Advanced Git: Functionality and Features

196

© 2016 Brent Laster

196

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

modules/mod1

gitdirHEAD

etc.

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

Submodules 2

Page 197: Advanced Git: Functionality and Features

197

© 2016 Brent Laster

197

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

modules/mod1

gitdirHEAD

etc.

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

3. git submodule update - actually clones repositories for submodules into the corresponding subdirectories and checks out the indicated commits for the containing project

Submodules 2

Page 198: Advanced Git: Functionality and Features

198

© 2016 Brent Laster

198

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

3. git submodule update - actually clones repositories for submodules into the corresponding subdirectories and checks out the indicated commits for the containing project

Submodules 2

Page 199: Advanced Git: Functionality and Features

199

© 2016 Brent Laster

199

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

3. git submodule update - actually clones repositories for submodules into the corresponding subdirectories and checks out the indicated commits for the containing project

submodulereference

Submodules 2

Page 200: Advanced Git: Functionality and Features

200

© 2016 Brent Laster

200

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

How do we clone a repository with submodules?1. git clone - puts in structure - but submodules areas are empty

2. git submodule init - puts submodule location information in superproject’s config file

Remote Repository

3. git submodule update - actually clones repositories for submodules into the corresponding subdirectories and checks out the indicated commits for the containing project

Note: Shortcuts - git submodule update --init and git clone --recursive or --recurse-submodules

submodulereference

Submodules 2

Page 201: Advanced Git: Functionality and Features

201

© 2016 Brent Laster

201

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

Incorporating updates to submodules:

Submodules 3

Page 202: Advanced Git: Functionality and Features

202

© 2016 Brent Laster

202

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:

Submodules 3

Page 203: Advanced Git: Functionality and Features

203

© 2016 Brent Laster

203

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can

Submodules 3

Page 204: Advanced Git: Functionality and Features

204

© 2016 Brent Laster

204

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

Submodules 3

Page 205: Advanced Git: Functionality and Features

205

© 2016 Brent Laster

205

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

Submodules 3

Page 206: Advanced Git: Functionality and Features

206

© 2016 Brent Laster

206

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

Submodules 3

Page 207: Advanced Git: Functionality and Features

207

© 2016 Brent Laster

207

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

Submodules 3

Page 208: Advanced Git: Functionality and Features

208

© 2016 Brent Laster

208

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/master

Submodules 3

Page 209: Advanced Git: Functionality and Features

209

© 2016 Brent Laster

209

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR

Submodules 3

Page 210: Advanced Git: Functionality and Features

210

© 2016 Brent Laster

210

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

Submodules 3

Page 211: Advanced Git: Functionality and Features

211

© 2016 Brent Laster

211

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

Page 212: Advanced Git: Functionality and Features

212

© 2016 Brent Laster

212

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

Page 213: Advanced Git: Functionality and Features

213

© 2016 Brent Laster

213

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 214: Advanced Git: Functionality and Features

214

© 2016 Brent Laster

214

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 215: Advanced Git: Functionality and Features

215

© 2016 Brent Laster

215

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 216: Advanced Git: Functionality and Features

216

© 2016 Brent Laster

216

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc. commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

.gitmodulesmod1

<current commit>

Submodules 3

3. To update: add, commit, and push reference changes.

Page 217: Advanced Git: Functionality and Features

217

© 2016 Brent Laster

217

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc. commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 218: Advanced Git: Functionality and Features

218

© 2016 Brent Laster

218

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 219: Advanced Git: Functionality and Features

219

© 2016 Brent Laster

219

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 220: Advanced Git: Functionality and Features

220

© 2016 Brent Laster

220

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 221: Advanced Git: Functionality and Features

221

© 2016 Brent Laster

221

@BrentCLaster

Remote Repository

main

superproject

Staging Area

Local Repository

Working Directory

main/mod1

submodule 1

Staging Area

Working Directory

Local Repository

modules/mod1

gitdirHEAD

etc.

commit pointed to by submodulereference

Head of branch

Remote Repository

submodulereference

new commitIncorporating updates to submodules:1. You can $ cd mod1; git checkout <branch> ; git pull

OR $ git pull --recurse-submodules; cd <module dir>; git merge origin/masterOR $ git submodule update --remote

2. Although module has been updated, references that the superproject has to it haven’t been updated

Submodules 3

3. To update: add, commit, and push reference changes.

Page 222: Advanced Git: Functionality and Features

222

© 2016 Brent Laster

222

@BrentCLaster

Submodule - Challenges Challenges around using submodules nearly always

involve keeping submodule content (and “current” commit) in sync with submodule references in superproject

If references are wrong, operations like “git submodule update” will backlevel submodule content to commits in reference

If these references are out of sync and that inconsistency is pushed to the remote for the superproject, then other users that pull that version of the superproject can end up back-leveling their submodules, even if they’ve updated their superproject before.

Page 223: Advanced Git: Functionality and Features

223

© 2016 Brent Laster

223

@BrentCLaster

Command: Subtrees

Purpose - Allows including a copy of a separate repository with your current repository

Use case - include a copy of a Git repository for one or more dependencies along with the original repository for a project

Syntax

Notes No links like a submodule - just a copy in a subdirectory Advantage - no links to maintain Disadvantage - extra content to carry around with your project

git subtree add -P <prefix> <commit> git subtree add -P <prefix> <repository> <ref> git subtree pull -P <prefix> <repository> <ref> git subtree push -P <prefix> <repository> <ref> git subtree merge -P <prefix> <commit> git subtree split -P <prefix> [OPTIONS] [<commit>]

Page 224: Advanced Git: Functionality and Features

224

© 2017 Brent Laster

224

Subtrees

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

Page 225: Advanced Git: Functionality and Features

225

© 2017 Brent Laster

225

Subtrees Adding a copy from a remote as

a subtree

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

Page 226: Advanced Git: Functionality and Features

226

© 2017 Brent Laster

226

Subtrees Adding a copy from a remote as

a subtree cd myproject

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

Page 227: Advanced Git: Functionality and Features

227

© 2017 Brent Laster

227

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

Page 228: Advanced Git: Functionality and Features

228

© 2017 Brent Laster

228

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 229: Advanced Git: Functionality and Features

229

© 2017 Brent Laster

229

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 230: Advanced Git: Functionality and Features

230

© 2017 Brent Laster

230

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 231: Advanced Git: Functionality and Features

231

© 2017 Brent Laster

231

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 232: Advanced Git: Functionality and Features

232

© 2017 Brent Laster

232

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 233: Advanced Git: Functionality and Features

233

© 2017 Brent Laster

233

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 234: Advanced Git: Functionality and Features

234

© 2017 Brent Laster

234

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Looking in the logs of the subproject will show the squashed history

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 235: Advanced Git: Functionality and Features

235

© 2017 Brent Laster

235

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Looking in the logs of the subproject will show the squashed history

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 236: Advanced Git: Functionality and Features

236

© 2017 Brent Laster

236

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Looking in the logs of the subproject will show the squashed history

To get the latest, use pull

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 237: Advanced Git: Functionality and Features

237

© 2017 Brent Laster

237

Subtrees Adding a copy from a remote as

a subtree cd myproject git subtree add --prefix

subproject --squash subproj.git master

Use “prefix” option to specify path for subproject

Use “squash” option to compress history from remote before adding it

branch (master) is optional

directory listing of myproject now shows subproj as subdirectory

Looking in the logs of the subproject will show the squashed history

To get the latest, use pull git subtree pull --prefix subproject

--squash subproj.git master

Remote Repository

myproject

superproject

superproject

Staging Area

(myproject)

Local Repository

Working Directory

Remote Repository(subproj)

myproject/subproject

subtree

Staging Area

Working Directory

Local Repository

Page 238: Advanced Git: Functionality and Features

238

© 2016 Brent Laster

238

@BrentCLaster

Subtrees - split split subcommand can be used to extract a subproject’s

content into a separate branch extracts the content and history related to <prefix> and

puts the resulting content at the root of the new branch instead of in a subdirectory

As output, Git prints out the SHA1 value for the HEAD of

the newly created tree Provides a reference to work with for that HEAD if needed New branch shows only the set of content from the subproject

that was split out (as opposed to content from the superproject).

Page 239: Advanced Git: Functionality and Features

239

© 2016 Brent Laster

239

@BrentCLaster

Subtree - create new project from split content Since can split out content from a subtree, may want to

transfer that split content into another project Very simple with Git

create new, empty project

pull contents of new branch into the new project (repository)

Page 240: Advanced Git: Functionality and Features

240

© 2016 Brent Laster

240

@BrentCLaster

Subtree - push

subtree command also supports a push subcommand This command does a split followed by an attempt to

push the split content over to the remote Example: the following command splits out the

subproject directory and then pushes it to the sub_origin remote reference and into a new branch named new branch:

Page 241: Advanced Git: Functionality and Features

241

© 2016 Brent Laster

241

@BrentCLaster

Command: (Interactive) Rebase Purpose - allows you to modify commits in the git history Use case - you need to make some kind of modification

to one or more commits in the repository (rewrite history)

Syntax

Notes - creates an interactive script/batch file to modify commits

Cautions - don’t use this on anything already pushed

Page 242: Advanced Git: Functionality and Features

242

© 2016 Brent Laster

242

@BrentCLaster

Interactive Rebase

Page 243: Advanced Git: Functionality and Features

243

© 2016 Brent Laster

243

@BrentCLaster

Interactive Rebase

Page 244: Advanced Git: Functionality and Features

244

© 2016 Brent Laster

244

@BrentCLaster

Interactive Rebase Choose set of

commits

Page 245: Advanced Git: Functionality and Features

245

© 2016 Brent Laster

245

@BrentCLaster

Interactive Rebase Choose set of

commits

Page 246: Advanced Git: Functionality and Features

246

© 2016 Brent Laster

246

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Page 247: Advanced Git: Functionality and Features

247

© 2016 Brent Laster

247

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Page 248: Advanced Git: Functionality and Features

248

© 2016 Brent Laster

248

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Page 249: Advanced Git: Functionality and Features

249

© 2016 Brent Laster

249

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Page 250: Advanced Git: Functionality and Features

250

© 2016 Brent Laster

250

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Page 251: Advanced Git: Functionality and Features

251

© 2016 Brent Laster

251

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Page 252: Advanced Git: Functionality and Features

252

© 2016 Brent Laster

252

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts

Page 253: Advanced Git: Functionality and Features

253

© 2016 Brent Laster

253

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts

Page 254: Advanced Git: Functionality and Features

254

© 2016 Brent Laster

254

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts

Page 255: Advanced Git: Functionality and Features

255

© 2016 Brent Laster

255

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts

Page 256: Advanced Git: Functionality and Features

256

© 2016 Brent Laster

256

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts

Page 257: Advanced Git: Functionality and Features

257

© 2016 Brent Laster

257

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts Commits are

updated

Page 258: Advanced Git: Functionality and Features

258

© 2016 Brent Laster

258

@BrentCLaster

Interactive Rebase Choose set of

commits Initiate

interactive rebase

Git presents initial script

Modify commands (save and exit)

Act on prompts Commits are

updated

Page 259: Advanced Git: Functionality and Features

259

© 2016 Brent Laster

259

@BrentCLaster

Command: Notes Purpose - Add additional information to objects in the Git repository

or look at such information Use case - At some point after making a commit, you may decide

that there are additional comments or other non-code information that you’d like to add with the commit - without changing the commit itself.

Syntaxgit notes [list [<object>]]git notes add [-f] [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]git notes copy [-f] ( --stdin | <from-object> <to-object> )git notes append [--allow-empty] [-F <file> | -m <msg> | (-c | -C) <object>] [<object>]git notes edit [--allow-empty] [<object>]git notes show [<object>]git notes merge [-v | -q] [-s <strategy> ] <notes-ref>git notes merge --commit [-v | -q]git notes merge --abort [-v | -q]git notes remove [--ignore-missing] [--stdin] [<object>… ]git notes prune [-n | -v]git notes get-ref

Page 260: Advanced Git: Functionality and Features

260

© 2016 Brent Laster

260

@BrentCLaster

Notes Create a note Create a note in a custom

namespace (add --ref)

View a note (for a specific revision)

List notes in log

Page 261: Advanced Git: Functionality and Features

261

© 2016 Brent Laster

261

@BrentCLaster

Command: grep Purpose - provides a convenient (and probably familiar) way to search for

regular expressions in your local Git environment. Use case - self-explanatory Syntax

git grep [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp] [-v | --invert-match] [-h|-H] [--full-name] [-E | --extended-regexp] [-G | --basic-regexp] [-P | --perl-regexp] [-F | --fixed-strings] [-n | --line-number] [-l | --files-with-matches] [-L | --files-without-match] [(-O | --open-files-in-pager) [<pager>]] [-z | --null] [-c | --count] [--all-match] [-q | --quiet] [--max-depth <depth>] [--color[=<when>] | --no-color] [--break] [--heading] [-p | --show-function] [-A <post-context>] [-B <pre-context>] [-C <context>] [-W | --function-context] [--threads <num>] [-f <file>] [-e] <pattern> [--and|--or|--not|(|)|-e <pattern>… ] [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>… ] [--] [<pathspec>… ]

Notes Several options are similar to OS grep options

Page 262: Advanced Git: Functionality and Features

262

© 2016 Brent Laster

262

@BrentCLaster

grep Default behavior - search for all instances of an expression across all tracked files in

working directory Search for all instances off expression “database” across all java files (note use of -- )

-p option tells Git to try and show header of method or function where search target was found

--break - make output easier to read --heading - prints filename above output

boolean operators search in staging area search in specific commit(s)

Page 263: Advanced Git: Functionality and Features

263

© 2016 Brent Laster

263

© 2016 Brent Laster@BrentCLaster

That’s all - thanks!