Hack your way to a good git history

Thinking of an old computer game

⚙️ The incredible machine

Git is handy

  • Less loss of work ;

  • Experiments in branches ;

  • History to use (locally and on platforms like GitHub).

Git resources for beginners

Git history

Small commits with informative messages

A mysterious line of code

a script with a mysterious line 'x <- x - 1'

Git blame

simplified diagram of Git blame: for each line in a script on the left we see who added it, when, with what commit message.

Git blame: click on the commit…

Git blame: click on the commit…

“Commit a bunch of files before lunch 🍝

Showing 145 changed files with 2,624 additions and 2,209 deletions.

Git blame: click on the commit…

“fix: adapt code to tool’s 0-indexing”

Showing 2 changed files with 3 additions and 2 deletions.

A bad idea 7 commits ago

Oh no, that idea from 7 commits ago is bad! Do we…

  • Manually remove the change;

  • Revert the commit that added the change?

Git revert

It only works well if the commit is small.

You doing a thing 3 days ago

a bunch of scripts, on them an emoji of a happy face wearing sunglasses

You doing the same thing today

a bunch of scripts, on them an emoji of a face sobbing

Git bisect: commit history

a series of emoji, the first one a cool face, then sleeping faces, then a sobbing face

Git bisect: explore commits optimally

a series of emoji, the first one a cool face, then sleeping faces, then a sobbing face. arrow above a sleeping face.

Git bisect: explore commits optimally

all faces until the arrow are now cool faces.

Git bisect: explore commits optimally

arrow above a different sleeping face

Git bisect: explore commits optimally

all faces after the arrow are now sobbing

Git bisect: explore commits optimally

arrow on last sleeping face

Git bisect: explore commits optimally

last sleeping face now sobbing

Git bisect result: a commit!

Git bisect result: a commit!

“Commit a bunch of files before workout 💪

Showing 145 changed files with 2,624 additions and 2,209 deletions.

Git bisect result: a commit!

“refactor: start using YAML”

Showing 2 changed files with 3 additions and 2 deletions.

Git bisect: let’s try it!

dir  <- withr::local_tempdir()
saperlipopette::exo_bisect(dir)

Git history

Your repository’s Git history should be like your Instagram profile grid.

Git history

“there’s no need for everyone to see the mistakes you made along the way”

Mike McQuaid, Git in Practice

How to get a nice(r) Git history

Another dimension to your work.

Work in branches

“The repeated amend”™️:
git commit --amend

What is git commit --amend

dir  <- withr::local_tempdir()
saperlipopette::exo_one_small_change(dir)

“The repeated amend”™️:
git commit --amend

https://happygitwithr.com/repeated-amend

  • First bit of work, git commit -m "feat: add cool thing"

  • Second bit of work, git commit --amend --no-edit

  • Done! git push

“The repeated amend”™️:
git commit --amend

  • git checkout -b 'feature-thing'

  • First bit of work, git commit -m "feat: add cool thing", git push

  • Second bit of work, git commit --amend --no-edit, git push -f

  • Done! git push -f

“Squash and merge”: click the right GitHub/GitLab button

a main branch with several commits, a feature branch with messy commits

“Squash and merge”: click the right GitHub/GitLab button

a main branch with several commits, including a new feature commit

“Squash and merge”: click the right GitHub/GitLab button

Example

“Start from scratch”

  • git reset --mixed Changes in the directory but not the Git history.

  • git add (--patch) Build commits in hindsight.

“Start from scratch”

dir  <- withr::local_tempdir()
saperlipopette::exo_reset(dir)

“Mix and match your commits”

git rebase -i

dir  <- withr::local_tempdir()
saperlipopette::exo_rebase_i(dir)

“Mix and match your commits”

Julia Evans’ rules for rebasing

Julia Evans’ post “git rebase: what can go wrong?”

Why have small, informative commits

Better history in particular for

  • git blame

  • git bisect

  • git revert

How to create better commits

No need to get it right on the first try

  • The Repeated Amend ™️

  • Squash and Merge PRs

  • Start from Scratch

  • Mix and Match your Commits

Practice safely in {saperlipopette}’s playgrounds!

saperlipopette as a CLI?

WIP: https://github.com/maelle/ohcrabgit

Use the tools you prefer!

What tools for Git ?

  • The terminal: never changes, and you learn the words.

  • RStudio IDE

  • Positron IDE, GitLens extension

  • Other IDEs

  • GitHub Desktop