Oops, Git!
How to recover from common mistakes

What is Git?

Git is a Version Control System (VCS).

What is Git?

This makes Git more like a mini filesystem with some incredibly powerful tools built on top of it, rather than simply a VCS [version control system].

Scott Chacon, Pro Git

Why Git?

after you commit a snapshot into Git, it is very difficult to lose, especially if you regularly push your database to another repository.

Scott Chacon

Also

📜 History to use

🌳 Branches

Actually…

Sometimes it hurts. 😱

Can you share ugly things that happened to you with Git?

Actually…

Sometimes it hurts. 😱

  • Push of secret data (API key for example).
  • Push of heavy data
  • Commits that embarrass us
  • Commits in the wrong branches

Our objectives today

💪 Prevent some problems

💪 Practice how to successfully get out of some horrible situations.

How we are going to work

In your usual Git environment.

Can you write how you use Git? Me: Positron+terminal sometimes.

RStudio

Show where Git is, the R console, the terminal.

Positron

Show where Git is, the R console, the terminal.

Other IDE

Do you dare to share?

Prevention: usethis::git_sitrep()

# Only Git today
usethis::git_sitrep(tool = "git", scope = "user")

https://usethis.r-lib.org/reference/git_sitrep.html

Prevention: knowing where you are

👀 Staging area

👀 Branch

👀 Remote (GitHub?) vs local (your computer)

How we are going to work

In temporary folders created by saperlipopette.

So we don’t break our important projects.

{saperlipopette}

A tool for using Git

An R package for creating exercises in your own folders.

https://docs.ropensci.org/saperlipopette/

install.packages(
  'saperlipopette', 
  repos = c('https://packages.ropensci.org', 'https://cloud.r-project.org')
)

saperlipopette

saperlipopette: R for creating Git exercises

  • R to create the exercise in another folder.

  • R in the new session to read the instructions.

  • Your usual tools to solve the Git problem!

saperlipopette: how to use the package

  • Call a function from an R session.

  • Go to the created folder (seen in the output).

  • Open R in this folder, read the instructions.

  • Work with your Git tools or the terminal.

  • Close the exercise session.

Let’s practice horrible situations!

I will make demos and then you will do them too.

My last commit is embarrassing 1/2

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

My last commit is embarrassing 2/2

folder <- withr::local_tempdir()
saperlipopette::exo_latest_message(folder)

See also saperlipopette::exo_check_editor().

My last commit is embarrassing

15 minutes to solve these exercises. 😈

folder1 <- withr::local_tempdir()
saperlipopette::exo_one_small_change(folder1)
folder2 <- withr::local_tempdir()
saperlipopette::exo_latest_message(folder2)

Break (5 minutes) ‍

My changes were wrong 1/2

folder <- withr::local_tempdir()
saperlipopette::exo_undo_commit(folder)

My changes were wrong 2/2

folder <- withr::local_tempdir()
saperlipopette::exo_revert_file(folder)

My changes were wrong

15 minutes to solve these exercises. 😈

folder1 <- withr::local_tempdir()
saperlipopette::exo_undo_commit(folder1)
folder2 <- withr::local_tempdir()
saperlipopette::exo_revert_file(folder2)

I committed on the wrong branch 1/2

folder <- withr::local_tempdir()
saperlipopette::exo_committed_to_main(folder)

I committed to the wrong branch 2/2

folder <- withr::local_tempdir()
saperlipopette::exo_committed_to_wrong(folder)

I committed to the wrong branch

15 minutes to solve these exercises. 😈

folder1 <- withr::local_tempdir()
saperlipopette::exo_committed_to_main(folder1)
folder2 <- withr::local_tempdir()
saperlipopette::exo_committed_to_wrong(folder2)

Break (5 minutes) ‍🍵

I did something horrible with rebase -i or some other barbarism

folder <- withr::local_tempdir()
saperlipopette::exo_time_machine(folder)

Solve it yourself in 10 minutes.

Prevention again

.gitignore

Demo

  • I create secret-folder/.

  • I see it in the staging area.

  • I add secret-folder to my .gitignore.

  • I don’t see it in the staging area anymore.

Vaccine

usethis::git_vaccinate()

https://usethis.r-lib.org/reference/git_vaccinate.html

What if I made a push of my mistake?

🚀 git push -f

But

🔥 not in shared branches

🔥 does not completely remove the old commit on GitHub https://github.com/ropensci-training/saperlipopette/pull/26

Conclusion

How to suffer less with Git?

Preventing problems: know where you are, use .gitignore (, don’t use Vim).

Learning how to get out of horrible situations.

Other situations we did not see

Thank you! 💙 Thanks to Dariia Mykhailyshyna and Yanina Bellini Saibene.

https://oops-git.netlify.app/

https://docs.ropensci.org/saperlipopette/

Git Resources: in English

Git resources: General books

Git resources: R

Git resources: others