mastering git -...
TRANSCRIPT
![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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/1.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/12.jpg)
.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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/19.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/21.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/24.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/30.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/32.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/33.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/34.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/35.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/36.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/37.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/38.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/39.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/40.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/41.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/42.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/43.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042601/5f76e7dd455c2e1c68349eeb/html5/thumbnails/44.jpg)
Q & A