Slide 1

Slide 1 text

Git 102 @timblair

Slide 2

Slide 2 text

All Things All People to Git is

Slide 3

Slide 3 text

Headache Including a massive

Slide 4

Slide 4 text

154 Commands how many do you know?

Slide 5

Slide 5 text

git-config

Slide 6

Slide 6 text

git-config 200+ Options

Slide 7

Slide 7 text

git-config git config option.name value Set value

Slide 8

Slide 8 text

git-config git config option.name Get value

Slide 9

Slide 9 text

git-config git config --unset option.name Unset value

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

git-config [user] name = Tim Blair email = [email protected] [color] diff = auto status = auto branch = auto [alias] co = checkout ci = commit st = status -sb

Slide 12

Slide 12 text

Aliases

Slide 13

Slide 13 text

Aliases Efficiency

Slide 14

Slide 14 text

Aliases Memory Space

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

git config alias.st status Internal Aliases Aliases

Slide 17

Slide 17 text

Internal Aliases Aliases Execute from the current path

Slide 18

Slide 18 text

git config alias.st "!git status" External Aliases Aliases

Slide 19

Slide 19 text

External Aliases Aliases Execute from the Repo root

Slide 20

Slide 20 text

co = checkout ci = commit cia = commit -a st = status -sb br = branch Aliases

Slide 21

Slide 21 text

pl = !git pull --rebase --prune && \ git submodule update --init Aliases

Slide 22

Slide 22 text

lg = log --color --graph --abbrev-commit \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d %Creset %s %Cgreen(%cr) %C(blue)<%aN>%Creset' Aliases

Slide 23

Slide 23 text

Aliases

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

snapshot = !git stash save "snapshot: $(date)" \ && git stash apply "stash@{0}" Aliases

Slide 27

Slide 27 text

Aliases nope = !git clean -dxf && git reset --hard

Slide 28

Slide 28 text

Aliases alias g='git'

Slide 29

Slide 29 text

Custom Commands

Slide 30

Slide 30 text

Custom Commands Alias++

Slide 31

Slide 31 text

Custom Commands 1 Create an executable file called git-foo

Slide 32

Slide 32 text

Custom Commands 2 Add the file to your path

Slide 33

Slide 33 text

Custom Commands 3 Call it like `git foo`

Slide 34

Slide 34 text

Custom Commands #!/bin/bash # Usage: git merge-point SHA ! 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

Slide 35

Slide 35 text

Dealing Remotes with

Slide 36

Slide 36 text

git pull fetch + merge is the default Dealing with Remotes

Slide 37

Slide 37 text

git pull fetch + rebase is better Dealing with Remotes

Slide 38

Slide 38 text

Dealing with Remotes set pull to rebase for new branches branch.autosetuprebase always

Slide 39

Slide 39 text

Dealing with Remotes merge instead of fast-forwarding merge.ff false

Slide 40

Slide 40 text

git push default is to push all branches Dealing with Remotes

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

git-up Rebase on pull + update all branches Dealing with Remotes github.com/aanand/git-up

Slide 44

Slide 44 text

Auto-Completion

Slide 45

Slide 45 text

Auto-Completion local and remote branch + tag names git subcommands file paths in the working directory and index common --long-options

Slide 46

Slide 46 text

git auto-completion for your bash Auto-Completion source `brew --prefix \ git`/etc/bash_completion.d/git-completion.bash

Slide 47

Slide 47 text

github.com/git/git/blob/master/contrib/completion/git-completion.zsh Auto-Completion zsh wrapper

Slide 48

Slide 48 text

oh-my-zsh plugin Auto-Completion gitfast

Slide 49

Slide 49 text

Auto-Correction

Slide 50

Slide 50 text

Auto-Correction $ git pusg git: 'pusg' is not a git command. See 'git --help'. ! Did you mean this? push

Slide 51

Slide 51 text

Auto-Correction correct common command typos in 10 deciseconds help.autocorrect 10

Slide 52

Slide 52 text

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...

Slide 53

Slide 53 text

Lost Commits

Slide 54

Slide 54 text

local history of branch tip updates Lost Commits git-reflog

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

verify connectivity of git objects Lost Commits git-fsck

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Amending Commits your

Slide 59

Slide 59 text

Amending Commits git commit --amend

Slide 60

Slide 60 text

Amending Commits git commit --amend --no-edit amend the last commit and reuse the commit message

Slide 61

Slide 61 text

Amending Commits git commit --amend -C HEAD amend the last commit and reuse all metadata

Slide 62

Slide 62 text

Revision Selection

Slide 63

Slide 63 text

Revision Selection SHA (long and short) ancestry commit message search time-based

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Switching Branches between

Slide 66

Slide 66 text

Switching Branches git checkout -

Slide 67

Slide 67 text

Switching Branches $ git checkout - Switched to branch 'master' ! $ git checkout - Switched to branch 'feature' ! $ git checkout - Switched to branch 'master'

Slide 68

Slide 68 text

git-diff

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

suppress whitespace differences git-diff git diff -w

Slide 71

Slide 71 text

show inline word differences git-diff git diff --word-diff

Slide 72

Slide 72 text

git-diff $ g diff --word-diff HEAD^ ! diff --git a/README.md b/README.md index 2dcccf8..15d5ca7 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ I'm likely to make a [-misteak-]{+mistake+} in here somewhere.

Slide 73

Slide 73 text

check for bad whitespace git-diff git diff --check

Slide 74

Slide 74 text

git-diff $ g diff --check ! README.md:1: trailing whitespace. +I'm likely to make a mistake in here somewhere.

Slide 75

Slide 75 text

Crafting Commits perfect

Slide 76

Slide 76 text

Less of a tip, more of a Crafting Commits plea

Slide 77

Slide 77 text

each commit should concisely represent a Crafting Commits single concept

Slide 78

Slide 78 text

that makes each commit easy to Crafting Commits understand

Slide 79

Slide 79 text

that makes each commit easy to Crafting Commits review

Slide 80

Slide 80 text

that makes each commit easy to Crafting Commits revert

Slide 81

Slide 81 text

an untidy history is fine locally commit regularly Crafting Commits

Slide 82

Slide 82 text

clean up before you push your history Crafting Commits

Slide 83

Slide 83 text

Crafting Commits is your friend git rebase -i

Slide 84

Slide 84 text

Each commit message should describe Crafting Commits what and why

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

"Add X," not "Adds X" or "Added X" Auto-Completion imperative present use the

Slide 87

Slide 87 text

make your commit history Crafting Commits tell a story

Slide 88

Slide 88 text

Autosquash

Slide 89

Slide 89 text

bf204f4 Add something I forgot f1c7e94 Add the other file 256beed Add some text to the README Autosquash your history

Slide 90

Slide 90 text

Autosquash clean up before pushing git rebase -i

Slide 91

Slide 91 text

pick 256beed Add some text to the README pick f1c7e94 Add the other file pick bf204f4 Add something I forgot Autosquash

Slide 92

Slide 92 text

bf204f4 squash! Add some text to the README f1c7e94 Add the other file 256beed Add some text to the README Autosquash prefixed commits

Slide 93

Slide 93 text

Autosquash helping you help yourself git rebase -i --autosquash

Slide 94

Slide 94 text

pick 256beed Add some text to the README squash bf204f4 squash! Add some text to the README pick f1c7e94 Add the other file Autosquash

Slide 95

Slide 95 text

And to Finish…

Slide 96

Slide 96 text

Quick Tips reuse recorded resolution git-rerere

Slide 97

Slide 97 text

Assisted binary search through commits git-bisect Quick Tips

Slide 98

Slide 98 text

strips trailing whitespace and collapses newlines git-stripspace Quick Tips

Slide 99

Slide 99 text

temporarily ignore changes to a file git update-index --assume-unchanged FILE Quick Tips --no-assume-unchanged to reverse it

Slide 100

Slide 100 text

create a commit with no code changes git commit --allow-empty Quick Tips

Slide 101

Slide 101 text

browse your local repo using gitweb git instaweb Quick Tips Requires lighttp: brew install lighttpd

Slide 102

Slide 102 text

search through all of history git log -S'STRING' Quick Tips

Slide 103

Slide 103 text

search through all of history git rev-list --all | xargs git grep -F 'STRING' Quick Tips

Slide 104

Slide 104 text

The End you may now applaud