Skip to content

Latest commit

 

History

History
286 lines (206 loc) · 5.86 KB

index.md

File metadata and controls

286 lines (206 loc) · 5.86 KB

Git Gud


Hi!

Gregoire
Grégoire PARIS
Software Engineer @ ManoMano


  • Linus Torvald's 2005 side project, "named" after him 😁
  • Maintained by Junio C Hamano a.k.a. gitster

👏


Committing hunks of patch

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 and checkoutrestore too

Always use it, avoid git add . (use git add -N?)


Committing hunks of patch

patch screenshot


Reviewing one more time

git commit --verbose
git commit -v # for short
  • is your second best friend
  • another chance to catch your mistakes before they get out

Reviewing one more time

patch screenshot


Writing a good commit message: the format 📝

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"

Writing a good commit message: the contents 📜

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!)

Writing a good commit message: the contents 📜

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

Writing a bad commit message: the GitHub UI user

update file.txt

  • don't use the web UI
  • organize a mass protest to change GitHub's behavior

Writing a bad commit message: the bug fixer

your opinion


Using the force ✋

git commit --amend
git push --force-with-lease

Proving your innocence: bisection 👼

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

Avoiding (git) conflicts 💥

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

Finding the culprit 🔍

git blame path/to/file/with/an/issue.php # the "I'm feeling lucky" way
git log -S 'var_dump' -p # the accurate way

How to avoid committing on the master branch

git branch -d master

Need to test something on "master"?

git switch --detach origin/master # -d for short

Basic hygiene 🚿

# 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

Undoing a catastrophic failure 💀

git reflog --date=iso
git show 2efadeb
git reset --hard 2efadeb

reflog screenshot


Understanding checkout's "inconsistent" API

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

Rebasing with ease

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

autosquash in action


Customizing the format of git rebase --interactive

git config --global rebase.abbreviateCommands true
git config --global rebase.instructionFormat "[%an @ %ar] %s"

git rebase with custom instruction format


Fixing coding standard issues properly

git rebase [--interactive] --exec "php-cs-fixer fix"

rebase --exec in action


Excluding files globally

git config --global core.excludesfile ~/.gitignore_global
echo ".DS_STORE_OR_WHATEVER_IT_IS" >> ~/.gitignore_global

Customizing the template directory

git config --global init.templatedir ~/path/to/my/templatedir

Reusing Recorded Resolutions of conflict merges

git config --global rerere.enabled true

Viewing your history, the cool way 😎

gource

Gource


Thanks!

Grégoire Paris

greg0ire