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

Practical Git(Hub)

Practical Git(Hub)

Git basics, best practices, tips & tricks. Talk given to my teammates at Kerad Games.

[email protected]

February 17, 2016
Tweet

Other Decks in Programming

Transcript

  1. Practical Git(Hub)

    View Slide

  2. Git basics
    Best practices
    Rebase & you
    Tips & tricks

    View Slide

  3. ❝ git gets easier once you
    get the basic idea that
    branches are
    homeomorphic
    endofunctors mapping
    submanifolds of a Hilbert
    space ❞
    -- The Internet

    View Slide

  4. ❝ SourceTree sucks ❞
    -- Me

    View Slide

  5. Git basics

    View Slide

  6. Git is…
    …a distributed revision control and source code
    management system.
    … not an evolution of SVN.
    … not easy.

    View Slide

  7. add
    am
    apply
    archimport
    archive
    bisect
    blame
    branch
    bundle
    cat-file
    check-attr
    check-ignore
    check-mailmap
    check-ref-
    format
    checkout
    checkout-
    index
    cherry
    cherry-pick
    citool
    clean
    clone
    commit
    commit-tree
    config
    count-objects
    cvsexportcomm
    it
    cvsimport
    cvsserver
    daemon
    describe
    diff
    diff-files
    diff-index
    diff-tree
    difftool
    fast-export
    fast-import
    fetch
    fetch-pack
    filter-branch
    fmt-merge-msg
    for-each-ref
    format-patch
    fsck
    gc
    get-tar-
    commit-id
    grep
    gui
    hash-object
    help
    http-backend
    http-fetch
    http-push
    imap-send
    index-pack
    init
    instaweb
    log
    ls-files
    ls-remote
    ls-tree
    mailinfo
    mailsplit
    merge
    merge-base
    merge-file
    merge-index
    merge-one-
    file
    merge-tree
    mergetool
    mktag
    mktree
    mv
    name-rev
    notes
    pack-objects
    pack-
    redundant
    pack-refs
    parse-remote
    patch-id
    prune
    prune-packed
    pull
    push
    quiltimport
    read-tree
    rebase
    receive-pack
    reflog
    relink
    remote
    repack
    replace
    request-pull
    rerere
    reset
    rev-list
    rev-parse
    revert
    rm
    send-email
    send-pack
    shell
    shortlog
    show
    show-branch
    show-index
    show-ref
    stash
    status
    stripspace
    submodule
    svn
    symbolic-ref
    tag
    unpack-file
    unpack-
    objects
    update-index
    update-ref
    update-
    server-info
    upload-
    archive
    upload-pack
    var
    verify-pack
    verify-tag
    whatchanged
    write-tree

    View Slide

  8. add
    am
    apply
    archimport
    archive
    bisect
    blame
    branch
    bundle
    cat-file
    check-attr
    check-ignore
    check-mailmap
    check-ref-
    format
    checkout
    checkout-
    index
    cherry
    cherry-pick
    citool
    clean
    clone
    commit
    commit-tree
    config
    count-objects
    cvsexportcomm
    it
    cvsimport
    cvsserver
    daemon
    describe
    diff
    diff-files
    diff-index
    diff-tree
    difftool
    fast-export
    fast-import
    fetch
    fetch-pack
    filter-branch
    fmt-merge-msg
    for-each-ref
    format-patch
    fsck
    gc
    get-tar-
    commit-id
    grep
    gui
    hash-object
    help
    http-backend
    http-fetch
    http-push
    imap-send
    index-pack
    init
    instaweb
    log
    ls-files
    ls-remote
    ls-tree
    mailinfo
    mailsplit
    merge
    merge-base
    merge-file
    merge-index
    merge-one-
    file
    merge-tree
    mergetool
    mktag
    mktree
    mv
    name-rev
    notes
    pack-objects
    pack-
    redundant
    pack-refs
    parse-remote
    patch-id
    prune
    prune-packed
    pull
    push
    quiltimport
    read-tree
    rebase
    receive-pack
    reflog
    relink
    remote
    repack
    replace
    request-pull
    rerere
    reset
    rev-list
    rev-parse
    revert
    rm
    send-email
    send-pack
    shell
    shortlog
    show
    show-branch
    show-index
    show-ref
    stash
    status
    stripspace
    submodule
    svn
    symbolic-ref
    tag
    unpack-file
    unpack-
    objects
    update-index
    update-ref
    update-
    server-info
    upload-
    archive
    upload-pack
    var
    verify-pack
    verify-tag
    whatchanged
    write-tree

    View Slide

  9. Making changes
    …or how add, commit, checkout works
    HEAD Index Working
    Directory
    checkout
    add
    commit reset

    View Slide

  10. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop HEAD

    View Slide

  11. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature
    HEAD
    git branch feature

    View Slide

  12. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature HEAD
    git branch feature
    git checkout feature

    View Slide

  13. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature HEAD
    git branch feature
    git checkout feature
    git add & git commit
    B

    View Slide

  14. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature HEAD
    git branch feature
    git checkout feature
    git add & git commit
    git add & git commit
    B C

    View Slide

  15. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature HEAD
    git branch feature
    git checkout feature
    git add & git commit
    git add & git commit

    B C
    D E

    View Slide

  16. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature
    HEAD
    git branch feature
    git checkout feature
    git add & git commit
    git add & git commit

    git checkout develop
    B C
    D E

    View Slide

  17. Grouping changes
    …or how to use branches via checkout, branch, merge
    A
    develop
    feature
    HEAD
    git branch feature
    git checkout feature
    git add & git commit
    git add & git commit

    git checkout develop
    git merge feature
    B C
    D E F

    View Slide

  18. Best practices
    Good

    View Slide

  19. commit
    commit often
    write good commit messages
    do not commit unrelated
    changes together

    View Slide

  20. commit
    commit often
    write good commit messages
    do not commit unrelated
    changes together

    View Slide

  21. branches
    anything in master is deployable
    anything in develop will be delivered
    in the next release *
    feature-branches is where
    we should commit

    View Slide

  22. branches
    anything in master is deployable
    anything in develop will be delivered
    in the next release
    feature-branches is where
    we should commit

    View Slide

  23. branches
    It’s ok to have different branch schemes, depends
    on client, project, technology…
    a common workflow > no workflow at all
    keep it simple or people won’t follow the scheme

    View Slide

  24. .gitignore
    know and use .gitignore
    committed files cannot be ignored (well, not easily)
    https://github.com/github/gitignore
    take a look at git config and .gitattributes as well

    View Slide

  25. git pull
    God kills a kitten
    every time you do this:
    Merge branch 'foobar' of github.com:keradgames/goldenmanager-ios
    into foobar

    View Slide

  26. git pull
    git pull is considered harmful
    git fetch origin
    git rebase origin/develop
    git config --global pull.ff only
    when changes can’t be applied fast-forward:

    View Slide

  27. git pull
    do it for the kittens
    if you need to solve conflicts when pulling, you are
    doing something wrong

    View Slide

  28. View Slide

  29. pull request
    why?
    we all make errors
    find logic bugs
    high quality code
    enforce readable code style
    enforce good commit history
    let people know what you are
    working on (even discuss future
    features)
    learn (both ways)
    fun

    View Slide

  30. pull request
    useful messages:
    “Can we make this more readable?”
    “What would be a better name for this method?”
    “This needs to be refactored into smaller methods”
    “Why did you choose this approach?”
    “”

    View Slide

  31. Rebase & you

    View Slide

  32. Historical revisionism
    A B C
    D E
    develop
    feature

    View Slide

  33. Historical revisionism
    A B C
    D E
    develop
    feature
    git checkout feature
    git rebase develop

    View Slide

  34. Historical revisionism
    A B C
    D E D’
    develop
    feature
    rebasing (1/2)

    View Slide

  35. Historical revisionism
    A B C
    D E D’ E’
    develop
    feature
    rebasing (2/2)

    View Slide

  36. Historical revisionism
    A B C
    D E D’ E’
    develop
    feature
    rebasing done!

    View Slide

  37. Historical revisionism
    A B C
    D E D’
    develop
    feature
    git rebase -i develop

    View Slide

  38. Historical revisionism
    A B C
    D E E’
    develop
    feature
    git rebase -i develop

    View Slide

  39. Historical revisionism
    A B C
    D E E’ D’
    develop
    feature
    git rebase -i develop

    View Slide

  40. © http://www.maneobjective.com/2013/10/danger-controversial-ingredients-in.html

    View Slide

  41. Rebasing dangers
    … or when not to use rebase.
    Don’t rebase public history.
    Think before rebasing long & old branches.

    View Slide

  42. Tips & tricks

    View Slide

  43. How do I…
    …find the commit that broke my project?
    git bisect start
    git bisect bad
    git bisect good
    # Bisecting: N revisions left to test after this
    (roughly log2(N) steps)
    git bisect run

    View Slide

  44. How do I…
    …save current changes for later use without committing
    because you need to do something else?
    git stash
    git stash list
    git stash pop
    git stash apply

    View Slide

  45. How do I…
    …revert a non-staged change?
    git checkout -- path/to/file/to/revert
    git checkout -- .
    …revert all non-staged changes (and nothing else)?


    View Slide

  46. How do I…
    …revert a staged change?
    git reset path/to/file/to/revert
    git reset
    …revert all staged changes (and nothing else)?

    View Slide

  47. How do I…
    …revert all staged and non-staged changes?
    (i.e.: all non-committed changes)
    git reset --hard

    View Slide

  48. How do I…
    …revert the last non-pushed commit?
    git reset HEAD^
    …revert/modify a previous non-pushed commit?
    git rebase -i

    View Slide

  49. How do I…
    …revert a pushed/public change?
    git revert

    View Slide

  50. How do I…
    …copy an existing commit (from another branch or
    another point in history) ?
    git cherry-pick

    View Slide

  51. How do I…
    …split changes in a single file into multiple commits?
    git add -p
    # for each hunk: y, n, s, e …
    git add -i

    View Slide

  52. How do I…
    …clean remote branches?
    git remote prune origin
    git config --global remote.origin.prune true
    git push origin --delete
    …delete a remote branch?

    View Slide

  53. How do I…
    … remove untracked files and directories?
    git clean -d -f

    View Slide

  54. How do I…
    … see changes introduced by a commit?
    git show
    … see changes introduced by a merge commit?
    git show -m

    View Slide

  55. How do I…
    … find who fucked up modified lines 42 to 84 of this file?
    git blame -L 42,84 path/to/file

    View Slide

  56. Questions?

    View Slide

  57. Thanks!

    View Slide