Me a few weeks ago 🤯

Bill Mill on Mastodon

Don’t use git checkout for anything:

  • use git switch to switch between branches

  • use git restore to restore files from a given tree

checkout has always been regrettably overloaded with those two capabilities, and nowadays you can avoid it by using commands designed for the purpose

How to hop on a branch 🐿️

git checkout <branch-name>

How to hop on a branch 🐿️

git switch <branch-name>

How to create a branch 🌱

git branch <branch-name>
git switch <branch-name>

or

git switch -c <branch-name>

How to undo changes

  • checkout restore

How to undo changes

  • checkout restore

  • revert

  • reset

Undo changes to some files

git checkout <commit-id> -- <path>

Undo changes to some files

withr::local_language("en")
parent_path <- withr::local_tempdir()
saperlipopette::exo_revert_file(
  parent_path = parent_path
)

Undo changes to some files

git restore <path> --source <commit-id>

Changes files but not the history.

Undo a commit 1/2

withr::local_language("en")
parent_path <- withr::local_tempdir()
saperlipopette::exo_undo_commit(
  parent_path = parent_path
)

Undo a commit 2/2

Without the new commit!

withr::local_language("en")
parent_path <- withr::local_tempdir()
saperlipopette::exo_undo_commit(
  parent_path = parent_path
)

Undo a commit

git revert <commit-id>

It creates a new commit that undoes the bad one.

Changes history and files… or just files (--no-commit)

Resetting 1/2

withr::local_language("en")
parent_path <- withr::local_tempdir()
saperlipopette::exo_committed_to_main(
  parent_path = parent_path
)

Resetting 2/2

withr::local_language("en")
parent_path <- withr::local_tempdir()
saperlipopette::exo_reset(
  parent_path = parent_path
)

Resetting

git reset --<mode> <commit-id>

Changes

  • history and files (--hard)

  • history but not the files (--mixed, --soft)

3 Git commands for undoing

  • git restore. Changes files to their state at a given commit.
  • git revert. New commit to counter another one.
  • git reset. Changes history (and files) to their state at a given commit.

https://jvns.ca/blog/2023/11/01/confusing-git-terminology/#reset-revert-restore

3 Git commands for undoing

Can change… files only history only both files and history
git restore x
git revert --no-commit default
git reset --mixed, --soft --hard

More undoing

  • git reset’s pathspec argument “For all specified files or directories, set the staged version to the version from the given commit or tree” 🤪
  • git rebase -i “edit”, “drop”
  • git commit --amend, git commit --amend --no-edit

Conclusion: bye git checkout

  • For changing/creating branches: git switch/git switch -c.
  • For restoring a file at a given state: git restore <path> --source <commit-id>.

How to lessen confusion