Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git Anti-Patterns: How To Mess Up With Git and Love it Again

Git Anti-Patterns: How To Mess Up With Git and Love it Again

These are the slides I presented at Devoxx Poland 2017 Conference.

Lemi Orhan Ergin

June 21, 2017
Tweet

More Decks by Lemi Orhan Ergin

Other Decks in Programming

Transcript

  1. @lemiorhan #DevoxxPL Platinum Sponsor: GITANTI PATTERNS How To Mess Up

    With Git and Love It Again LEMi ORHAN ERGiN Agile Software Craftsman, iyzico
  2. /lemiorhan lemiorhanergin.com @lemiorhan meetup.scturkey.org agileturkey.org slideshare.net/lemiorhan dev.iyzipay.com LEMi ORHAN ERGiN

    agile software craftsman @ iyzico agile practice lead at iyzico developing software since 2001 ex-Sony and ex-eBay employee consultant, architect, trainer, developer founder of Software Craftsmanship Turkey ex community leader of Agile Turkey got 1 million+ views for his slides
  3. Are you using git if all you do is commit-push-pull

    use dropbox instead ANTIPATTERN DANGER as if it is dropbox ?
  4. the way it keeps FILES & FOLDERS working copy staging

    area objects database repository the way it keeps REFERENCES directed acyclic graph keeping snapshots traversing graph branches, tags, heads git has 2 mechanisms
  5. Source Code Working Copy $ git init Object Database .git

    Folder / Object Database Cache Staging Area / The Index initializing repo
  6. Source Code Working Copy $ git init --bare Object Database

    .git Folder / Object Database Cache Staging Area / The Index Remote Upstream Repo / Remote Repo Server initializing bare repo
  7. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git add . preparing commits
  8. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git add . preparing commits
  9. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git commit -m “initial commit” commi!ing
  10. $ 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
  11. 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
  12. 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
  13. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git commit -m “initial commit” commi!ing
  14. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git remote add origin http://upstream.repo $ git push -u origin master pushing to remote
  15. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy new changes from others are pushed
  16. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git fetch fetching changes
  17. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git merge FETCHED_HEAD updating working copy
  18. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git pull git fetch + git merge ge!ing changes to source code
  19. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy want to update last commit
  20. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working 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
  21. 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
  22. 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
  23. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy want to squash or change last commits
  24. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git reset --mixed mixed reseting cannot be reached entries for commits, files and folders are removed from staging area
  25. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy want to get rid of last commits
  26. Object Database .git Folder / Object Database Cache Staging Area

    / The Index Remote Upstream Repo / Remote Repo Server Source Code Working Copy $ git reset --hard hard reseting cannot be reached removed from staging area removed from working copy
  27. are you brave enough to jump 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
  28. before merging do you validate commits back to source ?

    ANTIPATTERN DANGER code review, continuous integration, automated testing…
  29. merge and unmerge do you o!en want to just before

    releases ? master HEAD TAG/v13 version 14 $ git cherry-pick Every-Single-Commit-We-Want-To-Deploy
  30. the commit graph ? do you fully understand topic and

    shared branches, tracking branches, tags, HEADs, merge commits, reverted commits…
  31. STEP 0 split your big feature into mini shippable tasks

    master HEAD TOPIC ORIGIN/master refactorings tasks, like rest endpoints testable, deployable each task will have a branch, not a feature
  32. 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
  33. 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
  34. 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
  35. 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 if you branch is pushed already $ git push -f
  36. 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 if you branch is pushed already $ git push -f Sync source branch a!erwards $ git fetch origin master:master
  37. perfect later make it single commit $ git reset HEAD~3

    or $ git rebase -i HEAD~3 HEAD TOPIC STEP 3 ORIGIN/master tests are passing app is working code is reviewed (*) master
  38. $ git reset HEAD~3 (then commit) or $ git rebase

    -i HEAD~3 HEAD TOPIC STEP 3 ORIGIN/master perfect later make it single commit tests are passing app is working code is reviewed (*) master
  39. 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
  40. 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
  41. Continuous Integration validates master branch continuously master 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 TAMING THE POWER OF GIT make git the king 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
  42. use terminal GUIs are prison balls of developers it’s ok

    to use GUIs while checking diffs, resolving conflicts and viewing commit graph
  43. do not lose take extra care while using hard reset

    $ git reset --merge HEAD~ Use stash $ git stash save “updates local settings to keep db safe” $ git reset --hard HEAD~ $ git stash apply stash@{0} Create a new branch $ git checkout -b feature/PA-121 $ git add settings.xml $ git commit -m “adds new settings config” Use hard reset with merge and commit into it uncommited changes
  44. TOPIC HEAD MASTER long living branches with care handle Squash

    all commits in your topic branch and 
 make them available in working copy $ git merge --squash fix Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested $ git add . $ git commit -m “adds a feature” $ git branch -D topic
  45. long living branches with care handle TOPIC HEAD MASTER $

    git merge --squash fix Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested $ git add . $ git commit -m “adds a feature” $ git branch -D topic Squash all commits in your topic branch and 
 make them available in working copy
  46. stop adding prevent commits from being big ball of muds

    every change local change sets at JetBrains IDEs
  47. messages are read! # 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 .git-commit-template.txt use git commit templates to create be"er commit messages com t m i
  48. 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.
  49. master TAG/v1.1 login HEAD DETACHED HEAD STATE $ git rebase

    (and conflicts happen) $ git checkout HEAD~2 $ git checkout 43e3ab01 $ git checkout tags/v1.1 WHEN IT HAPPENS
  50. 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
  51. $ 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
  52. LEMi ORHAN ERGiN agile software craftsman @ iyzico /lemiorhan lemiorhanergin.com

    @lemiorhan DO NOT FORGET VIA MY DEVOXX MOBILE APP TO GIVE FEEDBACK TO LEMI