$30 off During Our Annual Pro Sale. View Details »

10 Git Anti Patterns You Should be Aware of

10 Git Anti Patterns You Should be Aware of

This is the slide deck I presented at ITAKE UnConf 2018.

Git is one of the most powerful tool in developers' toolbox. If you use it correctly, it dramatically increases productivity of developers and eliminates the waste products continuously. Developers cultivate a development culture on top Git most of the time.

It's powerful but its power is untamed. Many teams fall into several traps of misusing commands and therefore feel uncomfortable while using Git. We mess up Git history, the codebase and the whole preferred branching strategy in seconds. We use branches, merge/rebase strategies, creating commits in wrong ways. Even we never take committing paradigms into account while using Git.

As a software craftsman, I've been using Git for years and I've already educated Git to hundreds of developers in all levels. I'm so lucky; I had a chance to experience huge amount of anti-patterns in time. In this talk, I will talk about what those anti-patterns are and what should we do in order not to fall into them.

Lemi Orhan Ergin

June 07, 2018
Tweet

More Decks by Lemi Orhan Ergin

Other Decks in Programming

Transcript

  1. Lemi Orhan Ergin Agile Software Craftsman & Co-Founder, Craftbase
    GIT ANTI PATTERNS
    10
    YOU SHOULD BE AWARE OF

    View Slide

  2. LEMi ORHAN ERGiN
    agile software craftsman
    co-founder @ craftbase
    ex Sony, eBay, ACM, iyzico
    founder & co-organizer of SCTurkey 

    Turkish Software Craftsmanship Community
    /lemiorhan
    lemiorhanergin.com
    @lemiorhan

    View Slide

  3. Do you push every commit
    just a!er you create ?

    View Slide

  4. ANTIPATTERN DANGER
    TORTURING GIT BY PUSH ANTI-PATTERN
    Do you push every commit
    just a!er you create ?

    View Slide

  5. ANTIPATTERN DANGER
    if all you do is commit and immediate push
    you lose the opportunity to organize your commits
    via reset, rebase, commit with amend safely
    and you have to use force push at every time
    you organize your commits
    TORTURING GIT BY PUSH ANTI-PATTERN

    View Slide

  6. are you brave enough to
    jump to any commit ?

    View Slide

  7. are you brave enough to
    jump to any commit ?
    ANTIPATTERN DANGER
    BROKEN TIME MACHINE ANTI-PATTERN
    Nope. Usually tests do not pass, the application does
    not work, even the code does not compile in majority
    of commits.

    View Slide

  8. are you brave enough to
    jump to any commit ?
    BROKEN TIME MACHINE ANTI-PATTERN
    Do you want me to
    help you about
    finding the last
    commit that finalizes
    the feature?

    View Slide

  9. do you have
    loooooong living
    topic branches ?

    View Slide

  10. do you have
    loooooong living
    topic branches ?
    ANTIPATTERN DANGER
    LONG LIVING BRANCHES ANTI-PATTERN

    View Slide

  11. LONG LIVING BRANCHES ANTI-PATTERN
    do you have
    loooooong living
    topic branches ?
    welcome to
    merge hell
    LONG LIVING BRANCHES ANTI-PATTERN

    View Slide

  12. cherry-pick
    do you use
    to prepare releases ?

    View Slide

  13. cherry-pick
    do you use
    to prepare releases ?
    ANTIPATTERN DANGER
    CHERRY-PICK OVERDOSE ANTI-PATTERN

    View Slide

  14. master
    HEAD
    TAG/v13
    version 2.0
    $ git cherry-pick Every-Single-Commit-We-Want-To-Deploy
    CHERRY-PICK OVERDOSE ANTI-PATTERN
    FEATURE 12
    FEATURE 14
    FEATURE 13

    View Slide

  15. the commit graph ?
    do you fully understand
    when you read

    View Slide

  16. ANTIPATTERN DANGER
    LOST IN COMMIT GRAPH ANTI-PATTERN
    the commit graph ?
    do you fully understand
    when you read

    View Slide

  17. LOST IN COMMIT GRAPH ANTI-PATTERN
    the commit graph ?
    do you fully understand
    topic and shared branches, tracking branches, tags, HEADs, merge commits, reverted commits…
    LOST IN COMMIT GRAPH ANTI-PATTERN
    when you read

    View Slide

  18. Cure?

    View Slide

  19. Commit Early, Commit O!en
    Perfect Later, Publish Once
    PS: There are more than one way to achieve this

    View Slide

  20. split your big feature into
    mini shippable tasks
    HEAD
    TOPIC
    refactorings
    tasks, like rest endpoints
    red-green-refactor
    each task will have a
    branch, not a feature
    master
    ORIGIN/master
    STEP 0
    Our very first step is
    defining a strategy to
    keep branches short

    View Slide

  21. commit early
    commit o!en
    no need to compile
    no need for CI
    it’s only for versioning
    do not push
    HEAD
    TOPIC
    master
    ORIGIN/master
    STEP 1

    View Slide

  22. $ git fetch origin master:master
    HEAD
    TOPIC
    Sync source branch
    with one simple command
    master
    ORIGIN/master
    STEP 2

    View Slide

  23. $ git fetch origin master:master
    HEAD
    TOPIC
    Sync source branch
    with one simple command
    master
    ORIGIN/master
    STEP 2

    View Slide

  24. $ git merge master
    HEAD
    TOPIC
    Get incoming change sets
    from source to topic
    branch via merge
    master
    ORIGIN/master
    STEP 3

    View Slide

  25. $ git merge master
    HEAD
    TOPIC
    Get incoming change sets
    from source to topic
    branch via merge
    master
    ORIGIN/master
    STEP 3

    View Slide

  26. $ git merge master
    Get incoming change sets
    from source to topic
    branch via merge
    master
    ORIGIN/master
    HEAD
    TOPIC
    REGULARLY
    STEP 3

    View Slide

  27. $ git merge master
    Get incoming change sets
    from source to topic
    branch via merge
    master
    ORIGIN/master
    HEAD
    TOPIC
    REGULARLY
    STEP 3

    View Slide

  28. master
    ORIGIN/master
    HEAD
    TOPIC
    merge back to source
    by squashing all commits
    in topic branch
    $ git checkout master
    $ git merge --squash topic
    STEP 4

    View Slide

  29. master
    ORIGIN/master
    HEAD
    TOPIC
    Delete the topic branch
    $ git branch -D topic
    STEP 5

    View Slide

  30. master
    ORIGIN/master
    HEAD
    Delete the topic branch
    $ git branch -D topic
    STEP 5

    View Slide

  31. master
    ORIGIN/master
    HEAD
    Delete the topic branch
    $ git branch -D topic
    Now you can push
    STEP 5

    View Slide

  32. Continuous Integration
    validates master branch
    continuously
    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
    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
    TRUNK-BASED
    DEVELOPMENT
    let's make git king again

    View Slide

  33. Git commands?
    is it hard to learn

    View Slide

  34. use terminal
    GUIs are prison balls of developers
    it’s ok to use GUIs while checking diffs,
    resolving conflicts and viewing commit graph
    BUTTON ADDICT ANTI-PATTERN
    ANTIPATTERN DANGER

    View Slide

  35. does each of your commit have
    or you commit anything you have changed
    one special purpose?

    View Slide

  36. stop adding
    prevent commits from being big ball of muds
    every change
    BIG BALL OF MUD ANTI-PATTERN
    ANTIPATTERN DANGER

    View Slide

  37. BIG BALL OF MUD ANTI-PATTERN
    stop adding
    prevent commits from being big ball of muds
    every change
    local change sets at JetBrains IDEs
    BIG BALL OF MUD ANTI-PATTERN

    View Slide

  38. stop adding every change
    partial add : git add -p

    View Slide

  39. commit messages?
    do you really understand
    what’s wri!en in

    View Slide

  40. # WHAT
    # (this commit will...)
    # 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
    messages
    are
    commit
    documents!
    ANTIPATTERN DANGER
    AMBIGIOUS COMMIT MESSAGES ANTI-PATTERN

    View Slide

  41. git rebase
    git push --force
    A
    B
    C
    D
    git merge
    git pull
    Which of the following commands can cause
    duplicate commits in the commit graph?

    View Slide

  42. ANTIPATTERN DANGER
    Which of the following commands can cause
    duplicate commits in the commit graph?
    git rebase
    git push --force
    A
    B
    C
    D
    git merge
    git pull
    DUPLICATE COMMITS ANTI-PATTERN

    View Slide

  43. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    REBASE & FORCE PUSH
    $ git rebase master
    $ git push -f

    View Slide

  44. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    REBASE & FORCE PUSH
    $ git rebase master
    $ git push -f

    View Slide

  45. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    REBASE & FORCE PUSH
    $ git rebase master
    $ git push -f

    View Slide

  46. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    PULL A FORCE-PUSHED BRANCH
    $ git pull

    View Slide

  47. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    c5 c6
    c5’ c6’
    ORIGIN/FIX
    PULL A FORCE-PUSHED BRANCH
    $ git pull

    View Slide

  48. UPSTREAM
    LOCAL
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    c5 c6
    c5’ c6’
    c8
    master
    FIX
    HEAD
    ORIGIN/FIX
    PULL A FORCE-PUSHED BRANCH
    $ git pull

    View Slide

  49. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5 c6
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    PULL WITH REBASE
    $ git pull --rebase

    View Slide

  50. UPSTREAM
    LOCAL
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    HEAD
    master
    c1 c2 c3 c4 c7
    c5 c6
    c5’ c6’
    FIX
    PULL WITH REBASE
    $ git pull --rebase

    View Slide

  51. 1. TORTURING GIT BY PUSH
    2. BROKEN TIME MACHINE
    3. LONG LIVING BRANCHES
    4. TOO LATE TO VALIDATE
    5. CHERRY-PICK OVERDOSE
    6. LOST IN COMMIT GRAPH
    7. BUTTON ADDICT
    8. TRASH HOUSE
    9. BIG BALL OF MUD
    10. AMBIGIOUS 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. MERGE HELL
    29. F*UCK UP WITH FORCE PUSH
    30. LIVING AT DETACHED HEAD STATE

    View Slide

  52. RECAP
    what was really
    happened at that time?
    LET’S

    View Slide

  53. master
    TAG/v1.1
    login
    HEAD
    DETACHED HEAD STATE
    $ git checkout tags/v1.1
    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.
    ANTIPATTERN DANGER

    View Slide

  54. master
    TAG/v1.1
    login
    HEAD
    my poor friend
    worked for
    3 long days

    View Slide

  55. master
    TAG/v1.1
    login
    HEAD
    $ git checkout master
    when he moved to
    another branch, all
    commits were gone

    View Slide

  56. master
    TAG/v1.1
    login
    $ 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
    HEAD

    View Slide

  57. $ git reflog
    630ddad6e
    the one we are
    searching for
    master
    TAG/v1.1
    login
    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
    HEAD

    View Slide

  58. master
    typofix
    TAG/v1.1
    login
    $ git branch typofix 630ddad6e
    HEAD

    View Slide

  59. master
    typofix
    TAG/v1.1
    login
    $ git branch typofix 630ddad6e
    KEEP CALM, NOTHING WILL BE LOST
    HEAD

    View Slide

  60. LEMi ORHAN ERGiN
    agile software craftsman
    co-founder @ craftbase
    /lemiorhan
    lemiorhanergin.com
    @lemiorhan
    ANTIPATTERN DANGER
    thank you all!
    Feedback: bit.ly/lemiorhan

    View Slide