Site Tools


wiki:git-undoing

Table of Contents

Git Undoing changes

Undoing in git means discarding or reverting changes. The strategy depends on whether changes are staged, committed, or already pushed. Never force-push to a shared branch; instead use git revert to create a new commit that undoes the old one.

$ git restore <file>         # discard unstaged changes to a file
$ git restore --staged <file> # unstage a file (keep changes)
$ git revert <hash>          # create a new commit that undoes an old one
$ git reset --soft HEAD~1    # undo the last commit, keep changes staged
$ git reset --hard HEAD~1    # undo the last commit, discard changes

Use git restore for unstaged changes, git reset for uncommitted commits (only on your local branch), and git revert for pushing an undo to a shared branch. git reset --hard is destructive—it permanently discards changes—so use it carefully.

wiki/git-undoing.md · Last modified: by 127.0.0.1