Bye, Git checkout!
Don’t use
git checkoutfor anything:
use
git switchto switch between branches
use
git restoreto restore files from a given tree
checkouthas always been regrettably overloaded with those two capabilities, and nowadays you can avoid it by using commands designed for the purpose
git checkout <branch-name>
git switch <branch-name>
git branch <branch-name>
git switch <branch-name>
or
git switch -c <branch-name>
checkout restore
revert
reset
git checkout <commit-id> -- <path>
Changes files but not the history.
Without the new commit!
It creates a new commit that undoes the bad one.
Changes history and files… or just files (--no-commit)
Changes
history and files (--hard)
history but not the files (--mixed, --soft)
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
| Can change… | files only | history only | both files and history |
|---|---|---|---|
git restore |
x | ||
git revert |
--no-commit |
default | |
git reset |
--mixed, --soft |
--hard |
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-editgit switch/git switch -c.git restore <path> --source <commit-id>.