Git 102

Git 102

So you've learned the basics of Git, and you've started butting up against it. Maybe you can't quite remember all the different sub-commands and flags you use occasionally, or people have been moaning at you that your commit history is a bit messy. Ever deleted a branch accidentally and think it's gone forever? Or maybe you're using Git for editing text, and are fed up with diffs telling you a whole paragraph has changed when you've only changed a single character?

This deck gives a 100ft view of a bunch of commands and options within Git . It's not attempting to explain exactly how to use each thing, but will hopefully act as a jumping-off point for anyone who wants to either learn a bit more about Git, or become more efficient at using it.

This talk was given as part of the regular lunchtime "Tech Talks" series at Global Personals. A video of the talk is available at (35 minutes).


* Franchise:
* Ostrich Sans:
* Monaco (comes with OS X)


* Text is #FFFFFF at 80% opacity (100% for highlights)
* Background colours: #141A41, #092E0E, #2B0405
* BGs are in Keynote: Advanced Image Fill > Tile, colour at 50% opacity



April 24, 2014


  1. Git 102 @timblair

  2. All Things All People to Git is

  3. Headache Including a massive

  4. 154 Commands how many do you know?

  5. git-config

  6. git-config 200+ Options

  7. git-config git config value Set value

  8. git-config git config Get value

  9. git-config git config --unset Unset value

  10. git-config /etc/gitconfig (--system) ~/.gitconfig (--global) $GIT_DIR/config (--local) File Locations

  11. git-config [user] name = Tim Blair email = [color]

    diff = auto status = auto branch = auto [alias] co = checkout ci = commit st = status -sb
  12. Aliases

  13. Aliases Efficiency

  14. Aliases Memory Space

  15. vi ~/.gitconfig git config --global commit Or Aliases

  16. git config status Internal Aliases Aliases

  17. Internal Aliases Aliases Execute from the current path

  18. git config "!git status" External Aliases Aliases

  19. External Aliases Aliases Execute from the Repo root

  20. co = checkout ci = commit cia = commit -a

    st = status -sb br = branch Aliases
  21. pl = !git pull --rebase --prune && \ git submodule

    update --init Aliases
  22. lg = log --color --graph --abbrev-commit \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d %Creset

    %s %Cgreen(%cr) %C(blue)<%aN>%Creset' Aliases
  23. Aliases

  24. merged = !git branch -r --merged origin/master \ | grep

    -v master \ | grep -v HEAD | sed 's/ origin\\///g’ ! rm-branch = !sh -c \ \"(git branch -D $1 2>/dev/null || true) && \ git push origin :$1\" - ! rm-merged = !git merged | xargs -L1 git rm-branch Aliases
  25. publish = !git push -u origin \ `git symbolic-ref --short

    HEAD` ! finish = !OLD_BRANCH="`git symbolic-ref --short HEAD`" \ && git co master && git pl \ && git br -d "$OLD_BRANCH" Aliases
  26. snapshot = !git stash save "snapshot: $(date)" \ && git

    stash apply "stash@{0}" Aliases
  27. Aliases nope = !git clean -dxf && git reset --hard

  28. Aliases alias g='git'

  29. Custom Commands

  30. Custom Commands Alias++

  31. Custom Commands 1 Create an executable file called git-foo

  32. Custom Commands 2 Add the file to your path

  33. Custom Commands 3 Call it like `git foo`

  34. Custom Commands #!/bin/bash # Usage: git merge-point SHA <TARGET> !

    AIM=${2-master} ! git rev-list --ancestry-path $1..$AIM | grep -f <(git rev-list --first-parent $1..$AIM) | tail -n 1 | xargs git --no-pager show --shortstat
  35. Dealing Remotes with

  36. git pull fetch + merge is the default Dealing with

  37. git pull fetch + rebase is better Dealing with Remotes

  38. Dealing with Remotes set pull to rebase for new branches

    branch.autosetuprebase always
  39. Dealing with Remotes merge instead of fast-forwarding merge.ff false

  40. git push default is to push all branches Dealing with

  41. Dealing with Remotes Push only current branch to upstream push.default

  42. Dealing with Remotes new default setting in git 2.0 push.default

  43. git-up Rebase on pull + update all branches Dealing with

  44. Auto-Completion

  45. Auto-Completion local and remote branch + tag names git subcommands

    file paths in the working directory and index common --long-options
  46. git auto-completion for your bash Auto-Completion source `brew --prefix \

  47. Auto-Completion zsh wrapper

  48. oh-my-zsh plugin Auto-Completion gitfast

  49. Auto-Correction

  50. Auto-Correction $ git pusg git: 'pusg' is not a git

    command. See 'git --help'. ! Did you mean this? push
  51. Auto-Correction correct common command typos in 10 deciseconds help.autocorrect 10

  52. Auto-Correction $ git config --global help.autocorrect 10 ! $ git

    pusg WARNING: You called a Git command named 'pusg', which does not exist. Continuing under the assumption that you meant 'push' in 1.0 seconds automatically...
  53. Lost Commits

  54. local history of branch tip updates Lost Commits git-reflog

  55. Lost Commits 06770a0 HEAD@{0}: rebase finished: returning to refs/heads/master 06770a0

    HEAD@{1}: checkout: moving from master to 06770a0c b022a35 HEAD@{2}: checkout: moving from msg_id_refactor to master 912785c HEAD@{3}: commit: Refactor #message[_id] to avoid extra lookups b022a35 HEAD@{4}: checkout: moving from master to msg_id_refactor b022a35 HEAD@{5}: checkout: moving from cache-negative-lookups to master 704d342 HEAD@{6}: commit: Make conformity specs less brittle 2d9ea86 HEAD@{7}: commit: Just enabled pipelining once, not every time 024227d HEAD@{8}: commit: Clean up conformity checker 28d6a12 HEAD@{9}: commit: Break conformity checking logic out of member ed5b843 HEAD@{10}: cherry-pick: Use simpler class var references b022a35 HEAD@{11}: checkout: moving from master to cache-negative-lookups b022a35 HEAD@{12}: rebase finished: returning to refs/heads/master b022a35 HEAD@{13}: checkout: moving from master to b022a35e
  56. verify connectivity of git objects Lost Commits git-fsck

  57. Finds dangling commits and tags git fsck --lost-found Lost Commits

  58. Amending Commits your

  59. Amending Commits git commit --amend

  60. Amending Commits git commit --amend --no-edit amend the last commit

    and reuse the commit message
  61. Amending Commits git commit --amend -C HEAD amend the last

    commit and reuse all metadata
  62. Revision Selection

  63. Revision Selection SHA (long and short) ancestry commit message search

  64. Revision Selection git show HEAD # By alias git show

    12a86bc38 # By revision git show v1.0.1 # By tag git show feature132 # By branch name git show 12a86bc38^ # Commit parent git show 12a86bc38~2 # Grandparent git show master@{yesterday} # Time relative git show master@{2.hours.ago} # Time relative git show :/message # Commit message
  65. Switching Branches between

  66. Switching Branches git checkout -

  67. Switching Branches $ git checkout - Switched to branch 'master'

    ! $ git checkout - Switched to branch 'feature' ! $ git checkout - Switched to branch 'master'
  68. git-diff

  69. try a different diff algorithm git-diff diff.algorithm ... myers, patience,

  70. suppress whitespace differences git-diff git diff -w

  71. show inline word differences git-diff git diff --word-diff

  72. git-diff $ g diff --word-diff HEAD^ ! diff --git a/

    b/ index 2dcccf8..15d5ca7 100644 --- a/ +++ b/ @@ -1 +1 @@ I'm likely to make a [-misteak-]{+mistake+} in here somewhere.
  73. check for bad whitespace git-diff git diff --check

  74. git-diff $ g diff --check ! trailing whitespace. +I'm

    likely to make a mistake in here somewhere.
  75. Crafting Commits perfect

  76. Less of a tip, more of a Crafting Commits plea

  77. each commit should concisely represent a Crafting Commits single concept

  78. that makes each commit easy to Crafting Commits understand

  79. that makes each commit easy to Crafting Commits review

  80. that makes each commit easy to Crafting Commits revert

  81. an untidy history is fine locally commit regularly Crafting Commits

  82. clean up before you push your history Crafting Commits

  83. Crafting Commits is your friend git rebase -i

  84. Each commit message should describe Crafting Commits what and why

  85. Crafting Commits Short (50 chars or less) summary of changes

    More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. GitHub treats the first line as the title of the commit and the rest as the description. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, preceded by a single space, with blank lines in between, but conventions vary here
  86. "Add X," not "Adds X" or "Added X" Auto-Completion imperative

    present use the
  87. make your commit history Crafting Commits tell a story

  88. Autosquash

  89. bf204f4 Add something I forgot f1c7e94 Add the other file

    256beed Add some text to the README Autosquash your history
  90. Autosquash clean up before pushing git rebase -i

  91. pick 256beed Add some text to the README pick f1c7e94

    Add the other file pick bf204f4 Add something I forgot Autosquash
  92. bf204f4 squash! Add some text to the README f1c7e94 Add

    the other file 256beed Add some text to the README Autosquash prefixed commits
  93. Autosquash helping you help yourself git rebase -i --autosquash

  94. pick 256beed Add some text to the README squash bf204f4

    squash! Add some text to the README pick f1c7e94 Add the other file Autosquash
  95. And to Finish…

  96. Quick Tips reuse recorded resolution git-rerere

  97. Assisted binary search through commits git-bisect Quick Tips

  98. strips trailing whitespace and collapses newlines git-stripspace Quick Tips

  99. temporarily ignore changes to a file git update-index --assume-unchanged FILE

    Quick Tips --no-assume-unchanged to reverse it
  100. create a commit with no code changes git commit --allow-empty

    Quick Tips
  101. browse your local repo using gitweb git instaweb Quick Tips

    Requires lighttp: brew install lighttpd
  102. search through all of history git log -S'STRING' Quick Tips

  103. search through all of history git rev-list --all | xargs

    git grep -F 'STRING' Quick Tips
  104. The End you may now applaud