Git Version Rollback: How to Undo an Incorrect Commit and Retrieve Code
Git version rollback requires scenario-specific handling to avoid sensitive information leaks or code loss. For un-pushed incorrect commits, use `git reset`: `--soft` retains modifications and only undoes the commit, allowing re-submission of correct content; `--hard` completely discards modifications (irreversible, use with caution). For pushed incorrect commits, use `git revert` to create a new undo commit (safe for collaboration), e.g., `git revert HEAD` or specify a hash value. If code is accidentally deleted, use `git reflog` to view operation history, find the target commit hash, then restore with `git reset --hard <hash>`. Note: Prefer `--soft` for un-pushed commits, always use `revert` for pushed commits, avoid `--hard` in multi-person collaboration, and confirm the commit hash before operations.
Read More