git anti patterns - xp days ukraine 2017

80
$ git anti-patterns how to mess up with git and love it again LEMi ORHAN ERGiN Agile Software Craftsman, iyzico /lemiorhan @lemiorhan lemiorhanergin.com bit.ly/lemiorhan

Upload: lemi-orhan-ergin

Post on 22-Jan-2018

554 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Git Anti Patterns - XP Days Ukraine 2017

$ git anti-patterns

how to mess up with git and love it again

LEMi ORHAN ERGiNAgile Software Craftsman, iyzico

/lemiorhan @lemiorhan lemiorhanergin.com bit.ly/lemiorhan

Page 2: Git Anti Patterns - XP Days Ukraine 2017

/lemiorhan lemiorhanergin.com @lemiorhan

LEMi ORHAN ERGiNagile software craftsman @ iyzico ex-Sony, ex-eBay founder of Turkish Software Craftsmanship Community

Page 3: Git Anti Patterns - XP Days Ukraine 2017

git is powerfulbut you have to know using it properly

because of its internal structure

Page 4: Git Anti Patterns - XP Days Ukraine 2017

Are you using git

if all you do is commit-push-pulluse dropbox instead

ANTIPATTERN DANGER

as if it is dropbox ?

1DROPBOX-ISH GIT ANTI-PATTERN

Page 5: Git Anti Patterns - XP Days Ukraine 2017

learnno worries, I will cover how git behaves in 5 minutes

git workshow

Page 6: Git Anti Patterns - XP Days Ukraine 2017

the way it keeps FILES & FOLDERS.git folder, aka repository

working copy staging area

objects database

the way it keeps REFERENCESdirected acyclic graph keeping snapshots traversing graph branches, tags, heads

git has 2 mechanisms

Page 7: Git Anti Patterns - XP Days Ukraine 2017

Source CodeWorking Copy

you want to version changes

Page 8: Git Anti Patterns - XP Days Ukraine 2017

Source CodeWorking Copy

$ git init

Object Database.git Folder / Object Database

CacheStaging Area / The Index

initializing repo

Page 9: Git Anti Patterns - XP Days Ukraine 2017

Source CodeWorking Copy

$ git init --bare

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

initializing bare repo

Page 10: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git add . preparing commits

Page 11: Git Anti Patterns - XP Days Ukraine 2017

folder

folder

folder

file

file

file

$ git add . preparing commits

Page 12: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git add . preparing commits

Page 13: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git commit -m “initial commit” commi!ing

Page 14: Git Anti Patterns - XP Days Ukraine 2017

$ git commit -m “initial commit” commi!ing

folder

folder

folder

file

file

file

commit

branch

HEAD

For more details, refer to book Git Internals by Scott Chacon

Page 15: Git Anti Patterns - XP Days Ukraine 2017

folder

folder

folder

file

file

file

commit

$ git commit -m “second commit” commi!ing

folder

commit

branch

HEAD

folder

file

folder

For more details, refer to book Git Internals by Scott Chacon

Page 16: Git Anti Patterns - XP Days Ukraine 2017

folder

folder

folder

file

file

file

commit

$ git commit -m “third commit” commi!ing

folder

commit

folder

file

folder

folder

commit

branch

HEAD

file

For more details, refer to book Git Internals by Scott Chacon

Page 17: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git commit -m “initial commit” commi!ing

Page 18: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git remote add origin http://upstream.repo$ git push -u origin master pushing to remote

Page 19: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

new changes from others are pushed

Page 20: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git fetch fetching changes

Page 21: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git merge FETCHED_HEAD updating working copy

Page 22: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git pullgit fetch + git merge

ge!ing changes to source code

Page 23: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

want to update last commit

Page 24: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git reset --soft $ git commit --amend/ so" reseting

cannot be reached

Only the cache for the commit you reseted

is removed from staging area

for your current branch

Page 25: Git Anti Patterns - XP Days Ukraine 2017

folder

folder

folder

file

file

file

commit

folder

commit

folder

file

folder

folder

commit

branch

HEAD

file

For more details, refer to book Git Internals by Scott Chacon

$ git reset --soft $ git commit --amend/ so" reseting

Page 26: Git Anti Patterns - XP Days Ukraine 2017

folder

folder

folder

file

file

file

commit

folder

commit

folder

file

folder

folder

commit

branch

HEAD

file

For more details, refer to book Git Internals by Scott Chacon

$ git reset --soft $ git commit --amend/ so" reseting

Page 27: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

want to squash or change last commits

Page 28: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git reset --mixed mixed reseting

cannot be reached

entries for commits, files

and folders are removed from staging area

Page 29: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

want to get rid of last commits

Page 30: Git Anti Patterns - XP Days Ukraine 2017

Object Database.git Folder / Object Database

CacheStaging Area / The Index

RemoteUpstream Repo / Remote Repo

Server

Source CodeWorking Copy

$ git reset --hard hard reseting

cannot be reached

removed from staging area

removed from working copy

Page 31: Git Anti Patterns - XP Days Ukraine 2017

are you brave enough tojump to any commit ?

jump to a branch

create a branch from a commit

create a branch from a tag

ANTIPATTERN DANGER

$ git checkout feature/PA-121 $ git checkout -b fix/missing-sign-parameter 2449be8 $ git checkout -b hotfix/v1.1 tags/v1

2BROKEN TIME MACHINE ANTI-PATTERN

Page 32: Git Anti Patterns - XP Days Ukraine 2017

are you sure?

are you brave enough tojump to any commit ?

Page 33: Git Anti Patterns - XP Days Ukraine 2017

do you haveloooooong living

topic branches ?

ANTIPATTERN DANGER

3LONG LIVING BRANCHES ANTI-PATTERN

Page 34: Git Anti Patterns - XP Days Ukraine 2017

do you haveloooooong living

topic branches ?

welcome tomerge hell

Page 35: Git Anti Patterns - XP Days Ukraine 2017

before mergingdo you validate commits

back to source ?

ANTIPATTERN DANGER

code review, continuous integration, automated testing…

4TOO LATE TO VALIDATE ANTI-PATTERN

Page 36: Git Anti Patterns - XP Days Ukraine 2017

before mergingdo you validate commits

back to source ? are you sure?

Page 37: Git Anti Patterns - XP Days Ukraine 2017

merge and unmergedo you o!en need to

just before releases ?

ANTIPATTERN DANGER

5CHERRY-PICK OBSSESSION ANTI-PATTERN

Page 38: Git Anti Patterns - XP Days Ukraine 2017

merge and unmergedo you o!en need to

just before releases ?

master

HEAD

TAG/v13

version 14

$ git cherry-pick Every-Single-Commit-We-Want-To-Deploy

Page 39: Git Anti Patterns - XP Days Ukraine 2017

the commit graph ?do you fully understand

ANTIPATTERN DANGER

6DEATH ON COMMIT GRAPH ANTI-PATTERN

Page 40: Git Anti Patterns - XP Days Ukraine 2017

the commit graph ?do you fully understand

topic and shared branches, tracking branches, tags, HEADs, merge commits, reverted commits…

Page 41: Git Anti Patterns - XP Days Ukraine 2017

Cure?

Page 42: Git Anti Patterns - XP Days Ukraine 2017

Commit Early, Commit O"en Perfect Later, Publish Once

Page 43: Git Anti Patterns - XP Days Ukraine 2017

STEP 0

split your big feature into mini shippable tasks

master

HEAD

TOPIC

ORIGIN/master

refactorings tasks, like rest endpoints red-green-refactor

each task will have a branch, not a feature

Page 44: Git Anti Patterns - XP Days Ukraine 2017

STEP 1

commit early commit o!en no need to compile no need for CI it’s only for versioning

do not push

master

HEAD

TOPIC

ORIGIN/master

Page 45: Git Anti Patterns - XP Days Ukraine 2017

always pull with rebase

$ git pull --rebase origin master

to get forced pushes securely to rebase your commits

master

HEAD

TOPIC

STEP 2

ORIGIN/master

and do it o!en!

Page 46: Git Anti Patterns - XP Days Ukraine 2017

$ git pull --rebase origin master

master

HEAD

TOPIC

STEP 2

ORIGIN/master

always pull with rebaseto get forced pushes securely to rebase your commits

and do it o!en!

Page 47: Git Anti Patterns - XP Days Ukraine 2017

$ git pull --rebase origin master

master

HEAD

TOPIC

STEP 2

ORIGIN/master

if you branch is pushed already$ git push -f

always pull with rebaseto get forced pushes securely to rebase your commits

and do it o!en!

Page 48: Git Anti Patterns - XP Days Ukraine 2017

$ git pull --rebase origin master

master

HEAD

TOPIC

STEP 2

ORIGIN/master

if you branch is pushed already$ git push -f

Sync source branch a!erwards$ git fetch origin master:master

always pull with rebaseto get forced pushes securely to rebase your commits

and do it o!en!

Page 49: Git Anti Patterns - XP Days Ukraine 2017

perfect later make it single commit

$ git reset HEAD~3 $ git rebase -i HEAD~3 $ git merge --squash

HEAD

TOPIC

STEP 3

ORIGIN/master

tests are passing app is working code is reviewed (*)

master

Page 50: Git Anti Patterns - XP Days Ukraine 2017

HEAD

TOPIC

STEP 3

ORIGIN/master

perfect later make it single commit

tests are passing app is working code is reviewed (*)

master

$ git reset HEAD~3 $ git rebase -i HEAD~3 $ git merge --squash

Page 51: Git Anti Patterns - XP Days Ukraine 2017

Use feature flags/toggles

HEAD

TOPIC

STEP 4

ORIGIN/master

if feature should be disabled

merge back to source$ git checkout master $ git merge topic

master

Page 52: Git Anti Patterns - XP Days Ukraine 2017

Use feature flags/toggles

master

HEAD

TOPIC

STEP 4

ORIGIN/master

if feature should be disabled

merge back to source$ git checkout master $ git merge topic

Page 53: Git Anti Patterns - XP Days Ukraine 2017

Continuous Integration validates master branch continuouslymaster

HEAD

TOPIC

ORIGIN/master

Pull requests can be used to review code and to validate before merging back to master

Scrum tasks are mapped to commits, not stories

Github Flow can be used to govern overall

TRUNK-BASED DEVELOPMENT

let's make git great again

Feature flags should be used whenever possible

Commit early & o"en perfect later, publish once philosophy

Deliver frequently be prepared to send every single commit

Deleting branches a"er merge will make your commit graph readable

Page 54: Git Anti Patterns - XP Days Ukraine 2017

use terminalGUIs are prison balls of developers

it’s ok to use GUIs while checking diffs, resolving conflicts and viewing commit graph

BUTTON ADDICT ANTI-PATTERN7

ANTIPATTERN DANGER

Page 55: Git Anti Patterns - XP Days Ukraine 2017

stop adding

prevent commits from being big ball of mudsevery change

OMNIBUS BILL ANTI-PATTERN8

ANTIPATTERN DANGER

Page 56: Git Anti Patterns - XP Days Ukraine 2017

stop adding

prevent commits from being big ball of mudsevery change

local change sets at JetBrains IDEs

Page 57: Git Anti Patterns - XP Days Ukraine 2017

stop adding every changepartial add

Page 58: Git Anti Patterns - XP Days Ukraine 2017

messages areread!

# WHAT # <issue id> (this commit will...) <subject>

# WHY and HOW # Explain why this change is being made

# RELATED # Provide links or keys to any relevant issues or other resources

# REMEMBER # use lower case in the subject line # start with a verb in imperative tone in the subject line # do not end the subject line with a period # separate subject from body with a blank line # use the body to explain what and why vs. how # can use multiple lines with "-" for bullet points in body

$ git config --global commit.template ~/.git-commit-template.txt

$ git config --global commit.cleanup strip

use git commit templates to create be"er commit messages

comt

mi

F*CKING COMMIT MESSAGES ANTI-PATTERN9

ANTIPATTERN DANGER

Page 59: Git Anti Patterns - XP Days Ukraine 2017

messages areread!use git commit templates to create be"er commit messages

comt

mi

Page 60: Git Anti Patterns - XP Days Ukraine 2017

ANTIPATTERN DANGER

10ZOMBIE REBASE ANTI-PATTERN

Rebase ConflictsDeath by

master

HEAD

TOPIC

Page 61: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

HEAD

c5 c6c5 c6

git:(topic) $ git rebase masterrebase how

really workscommand

Page 62: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

HEAD

c5 c6c5 c6

git:(topic) $ git rebase masterrebase how

really workscommand

Page 63: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

HEAD

c5 c6c5 c6

git:(topic) $ git rebase masterrebase how

really workscommand

Page 64: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

HEAD

c5 c6c5 c6 c5'

REPLAY #1

git:(topic) $ git rebase master

orig_HEAD

rebase how

really workscommand

Page 65: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

c5 c6c5 c6 c5' c6c6'

git:(topic) $ git rebase master

HEAD

REPLAY #2

orig_HEAD

rebase how

really workscommand

Page 66: Git Anti Patterns - XP Days Ukraine 2017

TOPIC

master

c1 c2 c3 c4 c7

HEAD

c5 c6c5 c6 c5' c6c6'

git:(topic) $ git rebase master

REWIND

orig_HEAD

rebase how

really workscommand

Page 67: Git Anti Patterns - XP Days Ukraine 2017

master

c1 c2 c3 c4 c7

c5' c6c6'

git:(topic) $ git rebase master

TOPIC

HEAD

REWIND

rebase how

really workscommand

Page 68: Git Anti Patterns - XP Days Ukraine 2017

master

HEAD

TOPIC

YOU STILL WANT TO REBASE, RIGHT?

Page 69: Git Anti Patterns - XP Days Ukraine 2017

master

HEAD

TOPIC

$ git merge topicmerge to branches: merge with squash: squash and rebase:

$ git merge topic --squash

$ git reset HEAD~8 $ git commit $ git rebase master

Page 70: Git Anti Patterns - XP Days Ukraine 2017

1. DROPBOX-ISH GIT

2. BROKEN TIME MACHINE

3. LONG LIVING BRANCHES

4. TOO LATE TO VALIDATE

5. CHERRY-PICK OBSSESSION

6. DEATH ON COMMIT GRAPH

7. BUTTON ADDICT

8. TRASH HOUSE

9. OMNIBUS BILL

10. F*CKING COMMIT MESSAGES

11. ZOMBIE REBASE

12. CODE LOSING SYNDROME

13. MESS UP WITH THE ROLLBACK

14. CENTRALIZED GIT

15. MERGE FANATIC

16. BRANCH CEMETERY

17. UNCONTROLLED POWER

18. WEB OF REPOSITORIES

19. ORACLE SYNDROME

20. WAITING FOR HACKERS

21. EVIL MERGE

22. BRANCH OVERDOSE

23. CHUCKY THE COMMAND

24. NO HERO TO SAVE LIVES

25. DUPLICATE COMMITS

26. BIG FAT COMMIT

27. CONFLICT-FOBIA

28. LIVING AT DETACHED HEAD STATE

Page 71: Git Anti Patterns - XP Days Ukraine 2017

RECAP

what was reallyhappened at that time?

LET’S

Page 72: Git Anti Patterns - XP Days Ukraine 2017

master

TAG/v1.1

login

HEAD

DETACHED HEAD STATE

$ git checkout cecd95914 Note: checking out 'cecd95914'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

LIVING AT DETACHED HEAD STATE ANTI-PATTERN11

ANTIPATTERN DANGER

Page 73: Git Anti Patterns - XP Days Ukraine 2017

master

TAG/v1.1

login

DETACHED HEAD STATE

$ git rebase (and conflicts happen)

$ git checkout HEAD~2

$ git checkout 43e3ab01

$ git checkout tags/v1.1

WHEN IT HAPPENS

HEAD

Page 74: Git Anti Patterns - XP Days Ukraine 2017

master

TAG/v1.1

login

HEAD

poor little developer...

Page 75: Git Anti Patterns - XP Days Ukraine 2017

master

TAG/v1.1

login

HEAD

$ git checkout master

Page 76: Git Anti Patterns - XP Days Ukraine 2017

master

TAG/v1.1

login

HEAD

$ git reflog

aa67e3a2c HEAD@{0}: rebase finished: returning to refs/heads/fix/java-sql-Date-violates-LSR aa67e3a2c HEAD@{1}: rebase: fixes UnsupportedOperationException while calling toIstant() method of java.sql.Date a45f3c4e5 HEAD@{2}: rebase: checkout develop 630ddad6e HEAD@{3}: checkout: moving from develop to fix/java-sql-Date-violates-LSR b26cf7a1a HEAD@{4}: rebase: checkout develop 630ddad6e HEAD@{5}: checkout: moving from develop to fix/java-sql-Date-violates-LSR b26cf7a1a HEAD@{6}: pull: Fast-forward 8b59f8f50 HEAD@{7}: checkout: moving from fix/java-sql-Date-violates-LSR to develop 630ddad6e HEAD@{8}: rebase: updating HEAD

Page 77: Git Anti Patterns - XP Days Ukraine 2017

$ git reflog

630ddad6e

the one we are

searching for

master

TAG/v1.1

login

HEAD

aa67e3a2c HEAD@{0}: rebase finished: returning to refs/heads/fix/java-sql-Date-violates-LSR aa67e3a2c HEAD@{1}: rebase: fixes UnsupportedOperationException while calling toIstant() method of java.sql.Date a45f3c4e5 HEAD@{2}: rebase: checkout develop 630ddad6e HEAD@{3}: checkout: moving from develop to fix/java-sql-Date-violates-LSR b26cf7a1a HEAD@{4}: rebase: checkout develop 630ddad6e HEAD@{5}: checkout: moving from develop to fix/java-sql-Date-violates-LSR b26cf7a1a HEAD@{6}: pull: Fast-forward 8b59f8f50 HEAD@{7}: checkout: moving from fix/java-sql-Date-violates-LSR to develop 630ddad6e HEAD@{8}: rebase: updating HEAD

Page 78: Git Anti Patterns - XP Days Ukraine 2017

master

typofix

TAG/v1.1

login

HEAD

$ git branch typofix 630ddad6e

Page 79: Git Anti Patterns - XP Days Ukraine 2017

master

typofix

TAG/v1.1

login

HEAD

$ git branch typofix 630ddad6e

KEEP CALM, NOTHING WILL BE LOST

LEMi ORHAN ERGiN

Page 80: Git Anti Patterns - XP Days Ukraine 2017

LEMi ORHAN ERGiNagile software craftsman @ iyzico

/lemiorhan lemiorhanergin.com @lemiorhan

ANTIPATTERN DANGER

thank you all!Feedback: bit.ly/lemiorhan