Undoing Things and Good Habits

Harry · 14 Sep 2026 · 2 views
Advertisement
Advertisement

Undo before committing

git restore file.txt         # discard unstaged edits to a file
git restore --staged file.txt# unstage (keep the edits)

Fix the last commit

Forgot a file or mistyped the message? Amend it – as long as you have not pushed:

git add forgotten.txt
git commit --amend -m "Add login form and validation"

reset vs revert

These both “undo” commits but differently, and the distinction matters:

  • git reset moves the branch pointer back, rewriting history. Safe on commits you have not shared. --soft keeps changes staged, --hard throws them away.
  • git revert creates a new commit that undoes an earlier one, keeping history intact. This is the safe choice for commits already pushed to a shared branch.
git reset --soft HEAD~1   # undo last commit, keep changes staged (local only)
git revert a1b2c3d        # undo a pushed commit with a new commit

Stash work in progress

Need to switch branches but are not ready to commit? Shelve your changes:

git stash          # set unfinished changes aside
git switch main    # do the urgent thing
git switch feature/search
git stash pop      # bring the changes back

Habits that pay off

  • Commit small and often, one logical change per commit.
  • Write clear messages in the imperative: “Fix null check in parser”.
  • Never rewrite history (reset, force-push) on branches others use.
  • Pull before you start and before you push to reduce conflicts.

Key points

  • restore undoes uncommitted edits; commit --amend fixes the last (unpushed) commit.
  • reset rewrites local history; revert safely undoes shared commits.
  • stash shelves work in progress so you can switch context.
  • Small commits, clear messages, and never rewriting shared history keep a repo healthy.
Share this post:

Comments (0)

Please login or register to comment.