Reset vs revert, and when should I use stash?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Reset vs revert, and when should I use stash?
Definition:
Reset changes the current branch reference and may also change staged or working files depending on its mode. Revert records a new inverse change while preserving shared history. Stash stores local changes temporarily so another operation can proceed, but it should not replace committing important work.
Key points:
-
Reset moves the branch pointer; some modes also change staged or working files.
-
Revert adds a new commit that undoes earlier changes; useful on shared branches.
-
Stash temporarily stores local work; include untracked files explicitly when needed.
-
reset --hardcan discard tracked local changes; understand the mode before using it.
Comparison:
| Tool | Changes history how? | Typical use |
|---|---|---|
| Reset | Moves the current branch reference | Adjust local commits or staging |
| Revert | Adds an undo commit | Undo a shared change safely |
| Stash | Temporarily stores local work | Switch tasks before a commit |
- Check the working tree first; reset modes differ, and hard reset can discard local tracked changes.
Interview point:
- For an already shared bad commit, revert is usually easier for a team to follow because it adds history instead of rewriting it.
Adapted from 09_TESTING_GIT_DOCKER_CLOUD.md · “Reset vs revert, and when should I use stash?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.