Resetting, checking out & reverting

https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting

CommandScopeCommon use cases
git resetCommit-levelDiscard commits in a private branch or throw away uncommitted changes
git resetFile-levelUnstage a file
git checkoutCommit-levelSwitch between branches or inspect old snapshots
git checkoutFile-levelDiscard changes in the working directory
git revertCommit-levelUndo commits in a public branch by making a new commit
git revertFile-level(N/A)

Good Practice

  1. Prefer git reset when you wish to do some commit level changes, avoid it for file level. Learn the options --mixed(default), --soft and --hard.

  2. git reset can be history altering. In such cases,

    1. use git reflog to fix your mess up.
    2. use git reset -p to interactively change the history
  3. Prefer git switch over git checkout for switching between branches or creating new branches

  4. Using git reset or git checkout on a file level might be confusing as we need to know what they do by default. Instead use git restore, specifying the --source=<treeish> and whether to do changes in --worktree (default) or --staged

    |Command|Equivalent|
    |---|---|
    |git reset HEAD~2 ./readme|git restore --staged --source=HEAD~2 ./readme|
    |git checkout HEAD~2 ./readme|git restore [--worktree] --source=HEAD~2 ./readme|

    Also use patch mode -p to make changes interactively

  5. But remember both switch and restore commands are experimental as of git 2.43.1