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

Git on the front line

Git on the front line

In the years spent on the front line managing open source projects and applications developed by distributed teams, I've managed to gather quite a few Git stories from the trenches. In this practically oriented talk, I will share some patterns and practices I have collected while on the front.

Presented at:
- Codemotion 2017, Milan
- WPC 2017, Milan

Nicola Iarocci

November 11, 2017
Tweet

More Decks by Nicola Iarocci

Other Decks in Programming

Transcript

  1. 1
    Git on the front line
    Nicola Iarocci

    View Slide

  2. 2
    Who Am I
    Nicola Iarocci, a weirdo
    Python by Night
    I am the author and
    maintainer of a few
    Python open source
    projects: python-eve.org,
    python-cerberus.organd
    more.
    Microsoft MVP
    MongoDB Master
    I also happen to be a
    Microsoft MVP for Visual
    Studio and Development
    Technologies and a
    MongoDB Master.
    Communities
    I run DevRomagna, a
    developers community, and
    CoderDojo Ravenna, a
    coding club for kids.
    http:/
    /devromagna.org
    C# by Day
    I am the author of Sofware
    Gestionali Amica, a line of
    accounting and invoicing
    applications for Italian small
    businesses. Check it out at
    http:/
    /gestionaleamica.com

    View Slide

  3. 3
    one size does not fit all
    git workflows

    View Slide

  4. 4
    the main branch where the source code of HEAD
    always reflects a production-ready state
    origin/master
    main branch where HEAD always holds the latest
    delivered development changes, ready for next release
    origin/develop
    feature, release and hotfix branches. these always have
    a limited life time, since they will be removed eventually
    supporting branches
    an elegant mental model well suited for packaged
    software. Not ideal for frequent release cycles such as
    those we have in modern web development
    summary
    Git Flow
    a successful git branching model
    http:/
    /nvie.com/posts/a-successful-git-branching-model/

    View Slide

  5. 5
    the main branch where the source code of HEAD
    always reflects a production-ready state
    origin/master
    to work on something new, create a descriptively named
    branch off of master branch
    feature branch
    when you need feedback or help, or you think the
    branch is ready for merging, open a pull request
    pull request & review
    a lightweight, branch-based workflow that supports
    teams and projects where deployments are made
    regularly
    summary
    GitHub Flow
    widely adopted by the open source community
    http:/
    /scottchacon.com/2011/08/31/github-flow.html

    View Slide

  6. 6
    Streamline your git experience
    aliases

    View Slide

  7. 7
    git status, succinctly
    $ git st
    ## work
    M README.md
    $ git config —-global alias.st ‘status -sb’

    View Slide

  8. 8
    show last commit
    $ git last
    commit c828339f02f832818f868bbfe457e64bfcc3e64a
    Author: Nicola Iarocci
    Date: Thu Oct 19 10:28:42 2017 +0200
    first commit
    $ git config —-global alias.last ‘log -1 HEAD’

    View Slide

  9. 9
    quick branch checkout
    $ git co mybranch
    switched to branch ‘mybranch’
    $ git config —-global alias.co ‘checkout’
    Hint: git co - works like cd - but between branches (git checkout - works, too)

    View Slide

  10. 10
    checkout to a new branch
    $ git cob newbranch
    switched to a new branch ‘mybranch’
    $ git config —-global alias.cob ‘checkout -b’
    branch is created on the fly

    View Slide

  11. 11
    amend last commit
    $ git amend

    $ git config —-global alias.amend ‘commit —-amend’

    View Slide

  12. 12
    add staged changes to last commit
    $ git add .
    $ git fixup
    $ git config —-global alias.fixup ‘commit —-amend —-noedit’
    skip editing the commit message

    View Slide

  13. 13
    unstage changes
    $ git st
    ## work
    M README.md
    $ git unstage README.md
    Unstaged changes after reset:
    M README.md
    $ git config —-global alias.unstage ‘reset HEAD’
    pass no filename to unstage all, or use wildcard

    View Slide

  14. 14
    show staged changes
    $ git diffc
    diff —git a/README.md b/README.md
    index 2fd9bc4..889c3dc 100644
    --- a/README.md
    +++ b/README.md
    @@ -1 +1,2 @@
    TITLE
    +test
    $ git config —-global alias.diffc ‘diff —-cached’

    View Slide

  15. 15
    undo last commit and keep changes
    $ git r1
    Unstaged changes after reset:
    M README.md
    $ git config —-global alias.r1 ‘reset HEAD^’
    changes are left unstaged

    View Slide

  16. 16
    undo last commit, discard changes
    $ git rh1
    HEAD is now at c828339 first commit
    $ git config —-global alias.rh1 ‘reset -—hard HEAD^’
    use with caution

    View Slide

  17. 17
    add all changes including untracked,
    and commit with message
    $ git cm “my commit message”
    [mybranch ea88184] my commit message
    1 file changed, 1 insertion(+), 1 deletion(-)
    !git add -A && git commit -m

    View Slide

  18. 18
    add all changes excluding untracked,
    then commit as WIP
    $ git wip
    [mybranch ea88184] WIP
    1 file changed, 1 insertion(+), 1 deletion(-)
    !git add -u && git commit -m WIP

    View Slide

  19. 19
    add all changes including untracked,
    then commit as SAVEPOINT
    $ git save
    [mybranch ea88184] SAVEPOINT
    1 file changed, 1 insertion(+), 1 deletion(-)
    !git add -A && git commit -m SAVEPOINT

    View Slide

  20. 20
    resume work after WIP or SAVEPOINT
    $ git undo
    Unstaged files after reset:
    M README.md
    $ git config —-global alias.undo ‘reset HEAD^ —-mixed’
    functionally identical to ‘r1’ but makes more sense semantically

    View Slide

  21. 21
    add changes to commit, then wipe that commit
    $ git wipe
    HEAD is now at 8da9860 my commit message
    $ git reflog
    8da9860 [email protected]{0}: reset: moving to HEAD~1
    4998619 [email protected]{1}: commit: WIPE SAVEPOINT
    $ git reset —hard 4998619
    HEAD is now at 4998619 WIPE SAVEPOINT
    !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
    now working dir is clean, but I can still go back if need arises (via reflog)

    View Slide

  22. 22
    hierarchical, git-powered grep
    $ git g “event hooks”
    CHANGES
    1077:- [new] pre_ and ``pre__`` event hooks are
    now
    docs/features.rst
    1216:Database event hooks
    1219:Database event hooks work like request event hooks
    $ git config —-global alias.g ‘grep --break --heading --line-number’
    faster than standard grep

    View Slide

  23. 23
    show a tree with all branches
    $ git tree
    * bbde45f (HEAD -> expand_cerberus_registries) WIP
    * 3a25ea7 (origin/master, master) Merge branch ‘use_ordereddict_from_backport_collections_#1070’
    |\
    | * 9fb1a66 Changelog for #1070
    | * fa8da19 use OrderedDict from backport_collections
    |/
    * cee2f7e Merge branch 'support_for_decimal_type_#1048'
    |\
    | * 46af495 Minor changelog fixes for #1048
    | * 3bf1930 Support Decimal type MongoDB
    |/
    !git log --graph --decorate --all --oneline
    which brings us to the next topic…

    View Slide

  24. 24
    push/unpush current branch to remote
    $ git publish
    $ git unpublish
    In .gitconfig:
    branch-name = "!git rev-parse --abbrev-ref HEAD"
    publish = "!git push -u origin $(git branch-name)"
    unpublish = "!git push origin :$(git branch-name)"
    and set it to track upstream branch

    View Slide

  25. 25
    delete branches already merged to master
    $ git delete-merged-branches
    Deleted branch add_new_user_gravatar_links
    Deleted branch assign_unique_key_to_uploads
    Deleted branch remember_the_last_activity_per_user
    Deleted branch update_kaminari_to_thread_safe_version
    “!git branch —-merged master | grep -v '\\*' | xargs -n 1 git branch -d"

    View Slide

  26. 26
    shortcut to .config file
    $ git ec

    $ git config —-global alias.ec ‘config —global —e’

    View Slide

  27. 27
    get my dotfiles
    https:/
    /github.com/nicolaiarocci/dotfiles

    View Slide

  28. 28
    keep your history clean and consistent
    rebase
    Demo
    a documentation project

    View Slide

  29. 29
    reuse recorded resolutions to solve hunk conflicts
    rerere
    $ git config —-global rerere.enabled true
    https:/
    /git-scm.com/blog/2010/03/08/rerere.html

    View Slide

  30. 30
    shows all non-common commits in two branches (@antirez)
    rx script

    View Slide

  31. 31
    Aliases
    Streamline your workflow.
    Don’t Repeat Yourself.
    Consider sharing with team.
    Rebase
    Keep project history clean,
    compact and consistent. Not
    as hard as you thought.
    Keep Learning
    Study more advanced git
    features. Consider building
    your own scripts.
    Pick a Workflow
    Don’t be afraid to customize
    it just to suit your use case,
    project, and team.
    You Are a Craftsman
    sharpen your tools

    View Slide

  32. 32
    nicolaiarocci.com [email protected]
    @nicolaiarocci
    Thank You!
    Send feedback or get in touch at

    View Slide