Mastering git -...

44
Mastering git Sebastian Feldmann

Transcript of Mastering git -...

Page 1: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Mastering gitSebastian Feldmann

Page 2: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Mastering git

• Dos & Don’ts

• Tips & Tricks

• Hooks

Page 3: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Commit Messages

Page 4: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

whatthecommit.com

• Major fixup.

• Fixed tpyo

• One does not simply merge into master

• Best commit ever

Page 5: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Rules• Separate subject from body with one blank line

• Limit the subject line to 50 characters

• Do not end the subject line with a period

• Capitalize the subject line

• Wrap the body at 72 characters

• Use the body to explain what and why vs. how

Page 6: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Subject Line

Page 7: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Subject LineUse the imperative mood for the subject line like git is doing it

Mergebranch'myfeature'

Always complete the following: If applied, this commit will your subject line here

Oder in Deutsch: Dieser Commit deine Betreff Zeile

Page 8: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Subject Examples

• Refactor subsystem X for readability• Update getting started documentation• Remove deprecated methods• Prepare version 1.0.0

• Fixed bug with Y• Changing behavior of X• More fixes for broken stuff• Sweet new API methods

Page 9: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Full ExampleSummarizechangesinaround50charactersorless

Moredetailedexplanatorytext,ifnecessary.Wrapittoabout72charactersorso.Insomecontexts,thefirstlineistreatedasthesubjectofthecommitandtherestofthetextasthebody.Theblanklineseparatingthesummaryfromthebodyiscritical(unlessyouomitthebodyentirely);varioustoolslike`log`,`shortlog`and`rebase`cangetconfusedifyourunthetwotogether.

Explaintheproblemthatthiscommitissolving.Focusonwhyyouaremakingthischangeasopposedtohow(thecodeexplainsthat).Aretheresideeffectsorotherunintuitiveconsequensesofthischange?Here'stheplacetoexplainthem.

Furtherparagraphscomeafterblanklines.

-Bulletpointsareokay,too

-Typicallyahyphenorasteriskisusedforthebullet,precededbyasinglespace,withblanklinesinbetween,butconventionsvaryhere

Ifyouuseanissuetracker,putreferencestothematthebottom,likethis:

Resolves:#123Seealso:#456,#789

Page 10: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Tips & Tricks

Page 11: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Autocompletion

if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bashfi

Then in your .bash_profile add the following

curl -O https://raw.github.com/git/git/master/contrib/completion/git-completion.bashmv git-completion.bash .git-completion.bash

Download the completion script

Page 12: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

.git Template

• Default /usr/local/share/git-core/templates

• Custom hooks

• Custom excludes

git config --global init.templatedir '~/.git_template/template'

Page 13: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

OS Aliases

# I’m lazy as hellalias g='git'

# git status in a flashalias gs='git status’alias gss='git status -s’

# go to repository root directoryalias gr='[! -z `git rev-parse --show-cdup`] && cd `git rev-parse --show-cdup || pwd`'

Use system aliases

Page 14: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git aliases

$gitconfig--globalalias.unstage"resetHEAD"$gitconfig--globalalias.wtf"log-p"$gitconfig--globalalias.l"log--oneline--graph"$gitconfig--globalalias.b"branch"$gitconfig--globalalias.c"commit"$gitconfig--globalalias.p"pull—rebase"

Add via terminal or edit ~/.gitconfig

Page 15: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Reset Files$gitreset--hard{{some-commit}}

Return to a previous version and discard all changed

$gitreset{{some-commit}}

Return to a previous version, changes are unstaged

$gitreset--soft{{some-commit}}

Return to a previous version, changes are staged

Page 16: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Log--autor="Sebastian" Only show commits made by a certain author--name-only Only show names of files that changed--oneline Show commit data compressed to one line--graph Show dependency tree for all commits--reverse Show commits in reverse order (Oldest commit first)--after Show all commits that happened after certain date--before Show all commits that happened before certain data--stat Show all commits with some statistics

$gitlog--author="Sebastian"--after="1weekago"--oneline

Example

Page 17: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git logYou can use the regular less command to search

/{{your-search-here}}

Use lower case n to navigate to the next occurrence and upper case N to the previous one.

Page 18: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Ignore Whitespace

$gitdiff-w$gitblame-w

You can easily ignore whitespace for diff and blame

Page 19: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git stash

$gitstash

Store uncommitted changes

$gitstashlist

Show stashed changes

Page 20: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git stash

$gitstashpop

Apply latest stashed state and remove from list

$gitstashapplystash@{n}

Apply any stashed state and keep in list

$gitstashdropstash@{n}$gitstashclear

Clean up the stash

Page 21: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Demo

Page 22: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Partial Add

$gitadd-p

add only some changes in a file

Page 23: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Partial Add

y stage this hunk for the next commit

n do not stage this hunk for the next commit

d do not stage this hunk or any of the later hunks in the file

s split the current hunk into smaller hunks

e manually edit the current hunk

? print hunk help

Page 24: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Demo

Page 25: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git rebaseAhh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single line:

Do not rebase commits that exist outside your repository

If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.

–git book

Page 26: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git rebase

Merge remote-tracking branch ‘origin/master’

Committing on shared branches

„ „No big deal and completely safe, but still messes up the log history a bit.

$gitpull--rebase

Page 27: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git merge

Page 28: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git rebase

Page 29: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Demo

Page 30: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git amend

$gitaddpath/toFile/withCodingStandardFix.php$gitcommit--amend

Page 31: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

someone broke my stuff

but when

Page 32: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git bisectFind commits that break your code fast

$gitbisectstart$gitbisectgood{{some-commit}}$gitbisectbadHEAD

$gitbisectgood|bad

Test and flag the commit

Page 33: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git bisectGo back to the starting point

$gitbisectreset

$gitbisectlog

Get a summary report of last bisect run

Page 34: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Demo

Page 35: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

git hooks

Page 36: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Hooks

• post-checkout

• commit-msg

• pre-commit

• pre-push

Page 37: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

CaptainHook

• Easy to use

• Shareable configuration

• Simple to extend

https://github.com/sebastianfeldmann/captainhook

Page 38: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Usage

• Install via composer sebastianfeldmann/captainhook

• Manage hooks via CLI vendor/bin/captainhook

Page 39: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Configuration

• JSON configuration

• captainhook.json

Page 40: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Extend

• Commit message "Rules"

• Custom Actions

• Static methods

• Commands

Page 41: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Demo

Page 42: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

thank you

Please leave some feedback

Page 43: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Sebastian Feldmann

@movetodevnull

https://phpbu.de

sebastianfeldmann

Page 44: Mastering git - sebastian-feldmann.infosebastian-feldmann.info/talks/2016/20161025-masterin-git-ipc.pdf · Mastering git • Dos & Don’ts ... like git is doing it Merge branch 'myfeature'

Q & A