In Git usage, “Reset” and “Undo” are two common but easily confused operations. Both are used to correct mistakes, but their principles and scope of impact are completely different. This article explains the differences and applicable scenarios in the simplest language to help you get started quickly.

I. First, Understand Git’s “Areas”: Working Directory, Staging Area, Local Repository

Before understanding operations, remember Git’s three core areas (analogous to “drafts, folder, and archive box”):
- Working Directory: The actual files you see on your computer (e.g., newly written code).
- Staging Area: Temporarily stores files to be committed (files enter here after git add).
- Local Repository: Already committed historical versions (files are stored here after git commit).

II. Git Reset: Direct “Rewriting History” Reset Operation

The core of “Reset” is to move the HEAD (the pointer of the current branch) to a historical commit point and adjust the working directory/staging area according to parameters. It will directly modify the commit history and is suitable for “draft” scenarios where local changes have not been pushed.

1. Three Reset Modes and Applicable Scenarios

Mode Command Format Impact Range (Working Directory/Staging Area/Local Repository) Applicable Scenarios
Soft Reset git reset --soft <commit ID> Only move HEAD, staging area and working directory remain unchanged Want to modify the content/information of the most recent commit (e.g., author name was wrong), but keep the staged changes.
Mixed Reset git reset --mixed <commit ID> Move HEAD + reset staging area, working directory remains unchanged Undo staged files (e.g., accidentally executed git add but haven’t commit yet).
Hard Reset git reset --hard <commit ID> Move HEAD + reset staging area + reset working directory Thoroughly discard local modifications and return to a historical version (e.g., found previous commits were all wrong and want to “start over”).

2. Example to Understand Hard Reset

Suppose your recent commit history is: A → B → C (C is the latest commit). Now you want to go back to B:
- After executing git reset --hard B, HEAD points to B, and the working directory and staging area will be “cleared” to B’s state.
- (Note: The commit C will not disappear, but the latest branch pointer in your local repository no longer points to C. Subsequent git log may not show C, but Git will mark it as “unreferenced” until garbage collection.)

III. “Undo” Operation: Do Not Rewrite History, Keep Traces

“Undo” usually refers to not directly modifying history, but correcting errors through methods like “new commits” or “restoring files”. It is suitable for scenarios requiring historical preservation and multi-person collaboration.

1. git revert: Create a New Commit to “Roll Back” History

If a commit has been pushed to the remote (e.g., seen by team members), do NOT use Hard Reset (it will mess up the remote history)! Use revert instead:
- Principle: Create a new commit whose content is completely opposite to the commit to be undone.
- Command: git revert <commit ID> (e.g., git revert C will generate D, where D’s content undoes C’s modifications).
- Effect: The history will retain both C and D, and all team members will know “C has been reverted” after pulling the update.

2. git checkout: Restore Files or Branches

  • Restore a file: Recover a specific file from the staging area or historical commit.
  • From the staging area: git checkout -- <file> (e.g., after accidentally executing git add file.txt, but want to undo and restore to the pre-modified state).
  • From a historical commit: git checkout <commit ID> -- <file> (e.g., previously committed an incorrect file, now want to restore a file from the 3rd commit).
  • Restore a branch: git checkout <branch name> (switch to another branch, suitable for urgent bug fixes; use git stash to temporarily save current changes and apply them later with stash pop).

3. git commit --amend: Modify the Most Recent Commit

If you just committed a “work-in-progress” (missing file changes) or “wrong information”:
- Command: git commit --amend (opens an editor to modify the commit message; if the working directory has been modified, the changes will be directly added to the most recent commit).
- Effect: The original commit is “replaced” with a new one, and the history only shows the new commit.

4. git stash: Temporarily Store “Uncommitted Changes”

When you need to switch branches to handle an urgent task but have uncommitted modifications in the current working directory:
- git stash: “Package” and store uncommitted changes (does not affect commit history).
- git stash pop: Restore the stashed changes (equivalent to undoing the stash and putting the changes back into the working directory).

IV. Reset vs Undo Operations: Core Differences

Operation Type Principle Applicable Scenarios Precautions
Reset Directly rewrite HEAD pointer, discard intermediate history Local un-pushed “drafts” or “wrong commits” Using --hard forcefully will lose data; confirm modifications are safe to discard first.
Revert Create a new commit with “reverse operation” Pushed remote commits requiring historical preservation Does not change previous commits, only adds “undo records”.
Amend Replace the most recent commit Recently committed content or information needs modification Only affects the most recent commit; earlier commits remain unaffected.

V. Beginner Pitfalls and Avoidance

  1. Prefer Revert when unsure: Especially when the remote branch has been pushed, Revert is safe and preserves history.
  2. Use Reset boldly for local un-pushed changes: But avoid Hard Reset. First use git status to check if modifications are necessary, and adjust with --soft or --mixed step by step.
  3. Do not abuse --force: git push --force locally will force overwriting the remote branch, only use it if you are certain no one else is using the branch.

After the above content, you should be able to choose the appropriate operation based on the scenario. Remember: Git’s “Reset” “rewrites history”, while “Undo” “preserves traces”. The core is to clarify what problem you want to solve (recover files? modify commits? or roll back history?). With more practice, you’ll become more proficient!

Xiaoye