Grégoire PARIS
Software Engineer @ ManoMano
- Linus Torvald's 2005 side project, "named" after him 😁
- Maintained by Junio C Hamano a.k.a. gitster
👏
git add --patch
git add -p # for short
- is your best friend
- allows you to commit part of a file
- allows you to catch your mistakes before they get out
- works with
reset
andcheckout
restore
too
Always use it, avoid git add .
(use git add -N
?)
git commit --verbose
git commit -v # for short
- is your second best friend
- another chance to catch your mistakes before they get out
git commit -m "This is the worst possible way to commit" # do not do this
It should look like an e-mail 📧
- first line is the subject and should be less than 50 chars
- second line is streng verboten ⛔
- write a paragraph (the commit body) below, wrap it at 72 chars
Use vim, it enforces it!
git config --global core.editor "vim"
The commit message subject:
- should be about what you did
- completes "If applied, this commit will…"
- should not depend on JIRA or any other online resource (you only have 50 chars!)
The commit message body:
- should explain why you did what you did
- should sum up lengthy Git{hub,lab} / Bitbucket discussions
- may reference external bugtrackers, as a bonus
- may be the place to explain technical choices
- don't use the web UI
- organize a mass protest to change GitHub's behavior
git commit --amend
git push --force-with-lease
git bisect start
git bisect bad
git switch --detach ancient-commit
git bisect good
git bisect [good|bad|skip]
git bisect [good|bad|skip]
…
git bisect reset
- rarely needed
- very useful in dire situations
- is a good reason to write short commits
They happen when several people change the same file around the same line.
- when in doubt, use alphabetical order
- respect the Open/Close Principle
- write short lines (< 80 - 120 chars)
Finding the most edited files:
git log --pretty=format: --name-only | sort | uniq -c | sort -rg | head -10
git blame path/to/file/with/an/issue.php # the "I'm feeling lucky" way
git log -S 'var_dump' -p # the accurate way
git branch -d master
Need to test something on "master"?
git switch --detach origin/master # -d for short
# prune remote branches that are gone
git fetch --prune
# delete branches that are merged in origin/master
git branch --merged origin/master | xargs git branch -d
# delete branches that had an upstream branch in the past, but no longer do
git branch -v|grep -F '[gone]'|cut --field 3 --delimiter ' '|xargs git branch -D
git reflog --date=iso
git show 2efadeb
git reset --hard 2efadeb
git checkout 💩 # resets the working tree to that branch
git checkout README.md # forgets about changes in README.md 😕
git checkout 💩 README.txt # sets README.md to what it looks like in 💩
💡
git checkout 💩 # shortcut for git checkout 💩 .
git checkout README.md # shortcut for git checkout HEAD README.md
🎉 Replaced with switch and restore in recent versions 🎉
git switch 💩
git restore README.md # shortcut for git checkout HEAD README.md
git config --global rebase.autostash true
git config --global rebase.autosquash true
git commit --fixup b9acf57
git rebase --interactive b9acf57^1 # git rebase -i for short
git config --global rebase.abbreviateCommands true
git config --global rebase.instructionFormat "[%an @ %ar] %s"
git rebase [--interactive] --exec "php-cs-fixer fix"
git config --global core.excludesfile ~/.gitignore_global
echo ".DS_STORE_OR_WHATEVER_IT_IS" >> ~/.gitignore_global
git config --global init.templatedir ~/path/to/my/templatedir
- On my system, defaults to
/usr/share/git-core/templates
- I wrote an entire project about this, check it out: http://git-template.readthedocs.io
git config --global rerere.enabled true
gource