4.1 Undo Safely
Git can undo many things, but different commands carry different risks. At the beginning, the most important distinction is whether you are undoing uncommitted work or dealing with committed history.
Undo working tree changes
If a file is broken but not committed yet, inspect the state:
git statusThen decide whether to discard the change. Modern Git often suggests:
git restore file.txtThis restores file.txt to the state from the latest commit.
git restore discards uncommitted changes. Run it only when you are sure you do not need those edits.Undo committed content
If the mistake is already committed, especially if it has already been pushed, this is usually safer:
git revert <commit-id>revert creates a new inverse commit that cancels the effect of the old commit. It does not rewrite shared history, so it is better for collaboration.
Why be careful with reset?
git reset can move a branch pointer, and some modes also affect the staging area and working tree. It is powerful, but it can make beginners lose context.
In a team project, if a commit has already been pulled by others, do not casually use reset to rewrite that shared history.