Slide 1

Slide 1 text

~/ git --help Zhenlei Ji @zhenleiji

Slide 2

Slide 2 text

🗓 Agenda 1. A brief history of GIT 2. What is GIT? 3. GIT under the hood 4. Learn commands through examples 5. Android Studio + GIT 6. Configuration for your Monorepo 7. Final considerations

Slide 3

Slide 3 text

👩🏫 A brief history of GIT

Slide 4

Slide 4 text

Linus Torvalds

Slide 5

Slide 5 text

Why did he give it this name?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Linus Torvalds “the stupid content tracker”

Slide 8

Slide 8 text

Linus Torvalds 😀 “global information tracker”

Slide 9

Slide 9 text

Linus Torvalds 🤬 “goddamn idiotic truckload of sh*t”

Slide 10

Slide 10 text

🤔 What is GIT?

Slide 11

Slide 11 text

https://github.com/git/git “Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.”

Slide 12

Slide 12 text

https://github.com/git/git “Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.”

Slide 13

Slide 13 text

distributed control system

Slide 14

Slide 14 text

unusually rich command set add am archive bisect branch bundle checkout cherry-picky citool clean clone commit describe diff fetch format-patch gc gitk grep gui init log maintenance merge mv notes pull push range-diff rebase restore revert rm shortlog show sparse-checkout stash status submodule switch tag worktree

Slide 15

Slide 15 text

unusually rich command set add am archive bisect branch bundle checkout cherry-picky citool clean clone commit describe diff fetch format-patch gc gitk grep gui init log maintenance merge mv notes pull push range-diff rebase restore revert rm shortlog show sparse-checkout stash status submodule switch tag worktree and more…

Slide 16

Slide 16 text

🦸 😵💫

Slide 17

Slide 17 text

🤓 GIT under the hood

Slide 18

Slide 18 text

Git model SHA1 (Secure Hash Algorithm) 6 ff 87c4664981e4397625791c8ea3bbb5f2279a3

Slide 19

Slide 19 text

Git model SHA1 (Secure Hash Algorithm) 6 ff 87c4664981e4397625791c8ea3bbb5f2279a3

Slide 20

Slide 20 text

Git model SHA1 (Secure Hash Algorithm) 6 ff 87c4664981e4397625791c8ea3bbb5f2279a3

Slide 21

Slide 21 text

Internal Git model Tree aabbcc1 (SHA1) +aabbccf: blob = fi le.txt +ddee ff 1: blob = other.txt +aabbcc2: tree = folder Tree aabbcc2 (SHA1) + ff feee1: blob = nested.txt +dddee22: blob = other.txt Blob aabbccf (SHA1) +content = fi le.txt Blob ddee ff 1 (SHA1) +content = other.txt Blob ff feee1 (SHA1) +content = folder/nested.txt Blob dddee22 (SHA1) +content = folder/other.txt Commit abc123f (SHA1) +tree = aabbcc1 (SHA1) +author = Zhenlei +commiter = Zhenlei +parent = aaabbb5 +comment = A single commit Tag 7755 ff e (SHA1) +object = abc123f (SHA1) +type = commit +tagger = Zhenlei +comment = My tag comment

Slide 22

Slide 22 text

Git model .git/objects

Slide 23

Slide 23 text

https://learngitbranching.js.org/

Slide 24

Slide 24 text

Me “GIT is a graph structure of file systems with a bunch of commands to modify the graph.” In a nutshell

Slide 25

Slide 25 text

👨🏫 Learn commands through examples

Slide 26

Slide 26 text

1. Upload a new project to a remote repository

Slide 27

Slide 27 text

Create new project

Slide 28

Slide 28 text

Set default branch ~/ git con fi g --global init.defaultBranch main

Slide 29

Slide 29 text

Create git repository ~/ git init https://githooks.com/ 👈

Slide 30

Slide 30 text

Edit .gitignore

Slide 31

Slide 31 text

Edit .gitignore https://www.toptal.com/developers/gitignore

Slide 32

Slide 32 text

Edit .gitignore https://www.toptal.com/developers/gitignore

Slide 33

Slide 33 text

Add new or changed files to the staging area ~/ git add

Slide 34

Slide 34 text

Displays the state of the working directory and the staging area ~/ git status

Slide 35

Slide 35 text

Displays the state of the working directory and the staging area ~/ git status

Slide 36

Slide 36 text

Record the changes in the repository ~/ git commit

Slide 37

Slide 37 text

Open VIM 1. :wq 2. :x 3. shift+zz ❤ How to exit?

Slide 38

Slide 38 text

Set a default editor ~/ git con fi g --global core.editor

Slide 39

Slide 39 text

Create a connection to other repositories ~/ git remote add

Slide 40

Slide 40 text

Create a connection to other repositories ~/ git remote add Github Repo (origin) Sophia’s Repo (sophia) Your Repo

Slide 41

Slide 41 text

Upload local repository to a remote repository ~/ git push -u origin

Slide 42

Slide 42 text

Upload local repository to a remote repository ~/ git push -u origin

Slide 43

Slide 43 text

2. Create a new feature

Slide 44

Slide 44 text

Local Branches Remote Branches origin/featureA Sequence of commands C1 C3 main featureA* origin/main C2 C1 C3 C2 Remote Repository (github) HEAD Working on featureA

Slide 45

Slide 45 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA* origin/main C2 C1 C3 C2 Remote Repository (github) HEAD ~/ git checkout main

Slide 46

Slide 46 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 HEAD ~/ git checkout main Remote Repository (github)

Slide 47

Slide 47 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 C4 HEAD C4 ~/ git pull Remote Repository (github) git fetch git merge origin/main

Slide 48

Slide 48 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 C4 HEAD C4 ~/ git pull Remote Repository (github) git fetch git merge origin/main

Slide 49

Slide 49 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 HEAD C4 ~/ git checkout -b featureB featureB*

Slide 50

Slide 50 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 HEAD C4 ~/ git commit -m “C5” featureB* C5

Slide 51

Slide 51 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 HEAD C4 Update branch with the changes in main featureB* C5 1. git checkout main

Slide 52

Slide 52 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 C4 HEAD C4 Update branch with the changes in main featureB C5 1. git checkout main 2. git pull

Slide 53

Slide 53 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 C4 HEAD C4 Update branch with the changes in main featureB C5 1. git checkout main 2. git pull Remote Repository (github) git fetch git merge origin/main C6 C6

Slide 54

Slide 54 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main* featureA origin/main C2 C1 C3 C2 C4 HEAD C4 Update branch with the changes in main featureB C5 C6 C6 1. git checkout main 2. git pull 3. git checkout -

Slide 55

Slide 55 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 C4 Update branch with the changes in main featureB* C5 C6 C6 1. git checkout main 2. git pull 3. git checkout - HEAD

Slide 56

Slide 56 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 C4 Update branch with the changes in main featureB* C5 C6 C6 1. git checkout main 2. git pull 3. git checkout - 4. git merge main C7 HEAD

Slide 57

Slide 57 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA origin/main C2 C1 C3 C2 C4 C4 Update branch with the changes in main featureB* C5 C6 C6 1. git checkout main 2. git pull 3. git checkout - 4. git merge main C7 HEAD

Slide 58

Slide 58 text

Me “Do I really need local branch main?”

Slide 59

Slide 59 text

🙅

Slide 60

Slide 60 text

Me “Why not use an origin/main branch?” 🤯

Slide 61

Slide 61 text

Local Branches Local Remote Branches origin/featureA Sequence of commands C1 C3 main featureA* origin/main C2 C1 C3 C2 Remote Repository (github) HEAD Working on featureA git branch -D main

Slide 62

Slide 62 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA* origin/main C2 C1 C3 C2 Remote Repository (github) HEAD ~/ git fetch C4 git fetch

Slide 63

Slide 63 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA* origin/main C2 C1 C3 C2 HEAD ~/ git checkout -b featureB origin/main C4 featureB

Slide 64

Slide 64 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA origin/main C2 C1 C3 C2 ~/ git checkout -b featureB origin/main C4 featureB* HEAD

Slide 65

Slide 65 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA origin/main C2 C1 C3 C2 ~/ git commit -m “C5” featureB* C5 HEAD C4

Slide 66

Slide 66 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA origin/main C2 C1 C3 C2 Update branch with the changes in main featureB* C5 HEAD C4 C6 Remote Repository (github) git fetch 1. git fetch

Slide 67

Slide 67 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA origin/main C2 C1 C3 C2 Update branch with the changes in main featureB* C5 HEAD C4 C6 1. git fetch 2. git rebase origin/main

Slide 68

Slide 68 text

Local Branches Local Remote Branches origin/featureA Sequence of commands featureA origin/main C2 C1 C3 C2 Update branch with the changes in main featureB* C5 C4 C6 1. git fetch 2. git rebase origin/main C5’ HEAD

Slide 69

Slide 69 text

Many people “ What is the difference between merge and rebase?”

Slide 70

Slide 70 text

Merge

Slide 71

Slide 71 text

Merge ~/ git merge featureA C1 featureB* C3 C2 HEAD

Slide 72

Slide 72 text

Merge ~/ git merge featureA featureA C1 featureB* C2 C3 HEAD

Slide 73

Slide 73 text

Merge ~/ git merge featureA featureA C1 featureB* C2 C3 HEAD C4 What is the parent of C4? 🤔 C2? C3? Both?

Slide 74

Slide 74 text

https://twitter.com/HenryHo ff man/status/1442820054643625987 Merge

Slide 75

Slide 75 text

Merge

Slide 76

Slide 76 text

Merge

Slide 77

Slide 77 text

Rebase

Slide 78

Slide 78 text

Rebase ~/ git rebase featureA C1 featureB* C3 C2 HEAD C4

Slide 79

Slide 79 text

Rebase ~/ git rebase featureA featureA C1 featureB* C3 C2 HEAD C4

Slide 80

Slide 80 text

Rebase ~/ git rebase featureA featureA C1 featureB* C3 C2 HEAD C4 C3’

Slide 81

Slide 81 text

Rebase ~/ git rebase featureA featureA C1 featureB* C3 C2 HEAD C4 C3’ C4’

Slide 82

Slide 82 text

https://itnext.io/git-force-vs-force-with-lease-9d0e753e8c41

Slide 83

Slide 83 text

Rebase Interactive ~/ git rebase -i featureA C1 C2 C3 C4 C5 HEAD

Slide 84

Slide 84 text

Rebase Interactive ~/ git rebase -i HEAD~4 featureA C1 C2 C3 C4 C5 HEAD

Slide 85

Slide 85 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 HEAD

Slide 86

Slide 86 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 HEAD

Slide 87

Slide 87 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 HEAD

Slide 88

Slide 88 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 HEAD

Slide 89

Slide 89 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 C2’ HEAD

Slide 90

Slide 90 text

Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 C6 HEAD

Slide 91

Slide 91 text

C3’ Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 C6 HEAD

Slide 92

Slide 92 text

C34 Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 C6 HEAD

Slide 93

Slide 93 text

C34 Rebase Interactive ~/ git rebase -i C2 featureA C1 C2 C3 C4 C5 C6 HEAD C5’

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

3. When was the bug introduced?

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

~/ git bisect Use binary search to fi nd the commit that introduced a bug 1. git bisect start 2. git bisect bad 3. git bisect good 4. git bisect skip 5. git bisect reset https://git-scm.com/docs/git-bisect

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

~/ git bisect run validate.sh 1. 0 (zero) if the commit is good 2. 125 if the commit can’t be tested (skip) 3. Anything else (often 127) if the commit is bad https://git-scm.com/docs/git-bisect 😎

Slide 101

Slide 101 text

4. Houston, we have a problem!

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Restore the project’s history ~/ git re fl og

Slide 104

Slide 104 text

Restore the project’s history ~/ git reset --hard 2a88570fa7c https://www.atlassian.com/git/tutorials/rewriting-history/git-re fl og

Slide 105

Slide 105 text

🫶 Android Studio + GIT

Slide 106

Slide 106 text

Commit cmd+k

Slide 107

Slide 107 text

Push cmd+shift+k

Slide 108

Slide 108 text

Git tab cmd+9

Slide 109

Slide 109 text

Git tab cmd+9

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

👆

Slide 113

Slide 113 text

👆

Slide 114

Slide 114 text

GIT blame 👆

Slide 115

Slide 115 text

GIT conflicts

Slide 116

Slide 116 text

GIT conflicts

Slide 117

Slide 117 text

Pro tip Create shortcut for fetch and branches cmd+shift+a

Slide 118

Slide 118 text

Pro tip Create shortcut for fetch and branches opt+enter Fetch: opt+f Branches: opt+g

Slide 119

Slide 119 text

Pro tip Run fetch

Slide 120

Slide 120 text

Pro tip Show branches

Slide 121

Slide 121 text

Plugin Well integrated with Github

Slide 122

Slide 122 text

Plugin Well integrated with Github

Slide 123

Slide 123 text

iTerm + Oh My Zsh Add git plugin https://medium.com/ayuth/iterm2-zsh-oh-my-zsh-the-most-power-full-of-terminal-on-macos-bdb2823fb04c

Slide 124

Slide 124 text

iTerm + Oh My Zsh Add git plugin

Slide 125

Slide 125 text

https://www.youtube.com/watch?v=XMUnUotuvGw

Slide 126

Slide 126 text

🪨 Configuration for your monorepo

Slide 127

Slide 127 text

Github configuration

Slide 128

Slide 128 text

Github configuration

Slide 129

Slide 129 text

Enable auto-prune Delete the refs to branches that don't exist on the remote 1. git con fi g --global fetch.prune true 2. git con fi g remote.origin.prune true 1.73s -> 0.02s

Slide 130

Slide 130 text

Enable FSMonitor GIT fi le system monitor https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system-monitor/ Git version 2.37.0

Slide 131

Slide 131 text

Enable FSMonitor GIT fi le system monitor https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system-monitor/ Git version 2.37.0

Slide 132

Slide 132 text

Enable FSMonitor GIT fi le system monitor https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system-monitor/ Git version 2.37.0

Slide 133

Slide 133 text

Enable FSMonitor GIT fi le system monitor https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system-monitor/ Git version 2.37.0

Slide 134

Slide 134 text

Enable FSMonitor GIT fi le system monitor https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system-monitor/ Git version 2.37.0 1.73s -> 0.02s

Slide 135

Slide 135 text

🙌 Final considerations

Slide 136

Slide 136 text

There are more git commands to explore

Slide 137

Slide 137 text

Many ways to do the same thing

Slide 138

Slide 138 text

Many resources

Slide 139

Slide 139 text

https://learngitbranching.js.org/

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

https://git-scm.com/book/en/v2

Slide 142

Slide 142 text

Aristotle “The more you know, the more you know you don’t know.”

Slide 143

Slide 143 text

Thanks

Slide 144

Slide 144 text

@zhenleiji Zhenlei Ji

Slide 145

Slide 145 text

References • https://learngitbranching.js.org/ • https://book.git-scm.com/book/en/v2 • https://github.com/git/git • https://medium.com/gitopia/git-a-stupid-content-tracker-d0ef5b86865f • https://www.simplilearn.com/git-rebase-vs-merge-article • https://delicious-insights.com/en/posts/git-bisect/ • https://www.atlassian.com/git/tutorials/rewriting-history/git-re fl og • https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/ • https://github.blog/2022-06-29-improve-git-monorepo-performance-with-a- fi le-system- monitor/