Resetting, checking out & reverting
https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting
| Command | Scope | Common use cases |
|---|---|---|
git reset | Commit-level | Discard commits in a private branch or throw away uncommitted changes |
git reset | File-level | Unstage a file |
git checkout | Commit-level | Switch between branches or inspect old snapshots |
git checkout | File-level | Discard changes in the working directory |
git revert | Commit-level | Undo commits in a public branch by making a new commit |
git revert | File-level | (N/A) |
Good Practice
-
Prefer
git resetwhen you wish to do some commit level changes, avoid it for file level. Learn the options--mixed(default), --soft and --hard. -
git resetcan be history altering. In such cases,- use
git reflogto fix your mess up. - use
git reset -pto interactively change the history
- use
-
Prefer
git switchovergit checkoutfor switching between branches or creating new branches -
Using
git resetorgit checkouton a file level might be confusing as we need to know what they do by default. Instead usegit restore, specifying the--source=<treeish>and whether to do changes in--worktree(default) or--stagedCommand Equivalent git reset HEAD~2 ./readmegit restore --staged --source=HEAD~2 ./readmegit checkout HEAD~2 ./readmegit restore [--worktree] --source=HEAD~2 ./readmeAlso use patch mode
-pto make changes interactively -
But remember both switch and restore commands are experimental as of git 2.43.1