How Not to GIT

46
How Not To GIT Lucas Gomes <@x8lucas8x>

Transcript of How Not to GIT

Page 1: How Not to GIT

How Not To GITLucas Gomes <@x8lucas8x>

Page 2: How Not to GIT

Agenda● Who am I?● GIT Anti-Patterns

○ Manual Merging○ Copy-Paste Backup○ Dumb Bug Hunting○ Dumb Conflict

Resolution○ Jumbo Commit

2

○ Messing with History○ Divergent Master○ Self Reviewing

● References● Q & A

Page 3: How Not to GIT

Software EngineerPassionate Hacker ArchLinux ZealotFOSS Enthusiast

Startups, Big Data,AI, NoSQL, Python,C++/Qt, Git, Groovy,Cloud, IoT, and allthat jazz.

Lucas Lira Gomes

3

[email protected]

linkedin.com/in/x8lucas8x

facebook.com/x8lucas8x

youtube.com/X80lucas08X

twitter.com/x8lucas8x

last.fm/user/x8lucas8x

github.com/x8lucas8x

Page 4: How Not to GIT

GIT Anti-Patterns4

Page 5: How Not to GIT

Manual Merging

5

$ git checkout branchA...$ git commit...$ git pull origin master...$ echo ‘No pull requests this time!’...$ git checkout master...$ git rebase branchA...$ git push origin master...

Page 6: How Not to GIT

Manual Merging

6

● Problems○ “To Err is Human”

■ Automate what you can○ Permission management

■ Higher friction○ Jeopardise collaboration

■ Less code-review

Page 7: How Not to GIT

Manual Merging

7

A’sLocal

B’sLocal

C’sLocal

CentralRemote

Shared Repository

Page 8: How Not to GIT

Manual Merging

8

A’sLocal

B’sLocal

CentralRemote

A’sRemote

B’sRemote

ForkPull

Page 9: How Not to GIT

Manual Merging

9

● Solution○ Fork & Pull model

■ Use proper tools○ Avoid manual

merges/rebases■ Default to pull

requests

Page 10: How Not to GIT

Manual Merging

10

● Solution○ Fork to access

■ Create it first

$ git clone git@server_url:my_fork/remote.git

Page 11: How Not to GIT

Manual Merging

11

● Solution○ For updates

■ Forbid pushes■ Set up read-only

remote

$ git remote add central https://server_url/central/remote.git$ git remote set-url central --push "You shall NOT push!!!"

Page 12: How Not to GIT

Copy-Paste Backup

12

$ git status# On branch master# ...# modified: index.html#...$ cp index.html backup_index.html...$ git checkout -- index.html...$ git checkout branchBSwitched to a new branch 'branchB'

Page 13: How Not to GIT

Copy-Paste Backup

13

● Problems○ “To Err is Human”

■ Use proper tools

Page 14: How Not to GIT

Copy-Paste Backup

14

● Solution○ Stash it

$ git stashSaved working directory and index state "WIP on master: 049d078 added the index file"HEAD is now at 049d078 added the index file (To restore them type "git stash apply")

Page 15: How Not to GIT

Copy-Paste Backup

15

● Solution○ Stash it

$ git status# On branch masternothing to commit, working directory clean

$ git stash liststash@{0}: WIP on master: 049d078 added the index file

Page 16: How Not to GIT

Copy-Paste Backup

16

● Solution○ Stash it

$ git stash apply# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)## modified: index.html

$ git stash drop stash@{0}Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

Page 17: How Not to GIT

Dumb Bug Hunting

17

$ git commit...$ git commit...$ git commit...$ run app.py FATAL ERRORGonna cry?...$ diff HEAD~12...$ vim app.py...

Page 18: How Not to GIT

Dumb Bug Hunting

18

● Problems○ Not efficient○ More changed code

==More difficulty

Page 19: How Not to GIT

Dumb Bug Hunting

19

● Solution○ Manually Bitsec

$ git bisect start$ git bisect bad BAD_SHA1_HASH$ git bisect good GOOD_SHA1_HASHBisecting: 6 revisions left to test after this[b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing

Page 20: How Not to GIT

Dumb Bug Hunting

20

● Solution○ Manually Bitsec

$ git bisect goodBisecting: 3 revisions left to test after this[b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing

$ git bisect badBisecting: 1 revisions left to test after this[f71ce38690acf49c1f3c9bea38e09d82a5ce6014] drop exceptions table

Page 21: How Not to GIT

Dumb Bug Hunting

21

● Solution○ Manually Bitsec

$ git bisect goodb047b02ea83310a70fd603dc8cd7a6cd13d15c04 is first bad commitcommit b047b02ea83310a70fd603dc8cd7a6cd13d15c04Author: User1 <[email protected]>Date: Tue Jan 27 14:48:32 2015 -0800...

Page 22: How Not to GIT

Dumb Bug Hunting

22

● Solution○ Automate Bitsec

$ git bisect start BAD_SHA1_HASH GOOD_SHA1_HASH$ git bisect run make test arguments...

Page 23: How Not to GIT

Dumb Conflict Resolution

23

$ git pull origin master...$ git status# On branch branchA# You have unmerged paths.# ...# both modified: README.md#...$ vim README.md...

Page 24: How Not to GIT

Dumb Conflict Resolution

24

● Problems○ Not efficient○ Prone to typing errors

Page 25: How Not to GIT

Dumb Conflict Resolution

25

● Solution○ Use mergetool

$ git config --global merge.tool YOUR_DIFF_VIEWER

Page 26: How Not to GIT

Dumb Conflict Resolution

26

● Solution○ Use mergetool

$ git mergetoolMerging the files: README

Normal merge conflict for 'README': {local}: modified {remote}: modified

Hit return to start merge resolution tool (kdiff3):

Page 27: How Not to GIT

Dumb Conflict Resolution

27

● Solution○ Use mergetool (KDiff3)

Page 28: How Not to GIT

Dumb Conflict Resolution

28

● Solution○ Use mergetool (Meld)

Page 29: How Not to GIT

Dumb Conflict Resolution

29

● Solution○ Use mergetool

■ emerge■ gvimdiff■ vimdiff

Page 30: How Not to GIT

Jumbo Commit

30

$ git add src/views/....$ git add src/models/....$ git add src/delegates/....$ git add images/....$ git commit -a ‘Biggest commit ever!’...

Page 31: How Not to GIT

Jumbo Commit

31

● Problems○ Long-term results

■ Team○ Review procrastination

■ Higher cognitive load○ Pulls are more prone to

conflict

Page 32: How Not to GIT

Jumbo Commit

32

● Solution○ Divide and conquer

■ Create granular commits

Page 33: How Not to GIT

Messing with History

33

$ git pull origin master...$ git log...$ git commit --amend...$ git push origin master...# ! [rejected] master -> master (non-fast-forward)...

Page 34: How Not to GIT

Messing with History

34

● Problems○ Cannot push○ If forced (push -f)

■ Break others history

■ Lose commits

Page 35: How Not to GIT

Messing with History

35

● Solution○ Avoid messing with

pushed history■ On shared

repositories only

Page 36: How Not to GIT

Messing with History

36

● Solution○ Use Git Revert

$ git revert DESIRED_SHA1_HASH

Page 37: How Not to GIT

Divergent Master

37

$ git checkout master...$ git rebase branchA...$ git status...# Your branch and 'origin/master' have diverged...

Page 38: How Not to GIT

Divergent Master

38

● Problems○ Prone to mix different

branches commits■ Master is the

ramification point

Page 39: How Not to GIT

Divergent Master

39

● Solution○ Use master as a carbon copy

$ git checkout -b branchASwitched to a new branch 'branchA'

$ git branch -D master...

Page 40: How Not to GIT

Divergent Master

40

● Solution○ Use master as a carbon copy

$ git branch -abranchAremotes/central/master

$ git checkout remotes/central/master...

Page 41: How Not to GIT

Divergent Master

41

● Solution○ Use master as a carbon copy

$ git checkout -b masterSwitched to a new branch ‘master’

Page 42: How Not to GIT

Self Reviewing

42

$ git push origin branchA...$ echo “Lets create a pull request!”...

Page 43: How Not to GIT

Self Reviewing

43

● Problems○ Team specialisation

■ “Not my code"○ Less opportunity

■ Learning■ Training

Page 44: How Not to GIT

Self Reviewing

44

● Solution○ Assign pull requests

to others

Page 45: How Not to GIT

References1. https://www.git-scm.com/2. http://www.atlassian.com/git/tutorials/

45

Page 46: How Not to GIT

How Not To GITLucas Gomes <@x8lucas8x>