Check what you are about to undo
Git can undo changes at several different stages. A file you have edited but not staged is different from a staged file, and both are different from a commit. Choosing a command starts with identifying where the unwanted change lives. This guide assumes you have a local Git repository and a terminal. The restore command requires Git 2.23 or later; check your installed version with git --version.
Before changing anything, run git status. To inspect unstaged edits, run git diff. To inspect staged edits, run git diff --staged. For recent commits, run git log -n 5 --oneline. If you might want your work later, copy the relevant files elsewhere or make a temporary commit first. An undo command is easier to use confidently when you know exactly what it will replace.
Use restore for changes to files
Use git restore path/to/file to discard unstaged edits to a tracked file. Git replaces the file’s working-copy contents with its staged version. If the file has no staged changes, that is usually the version in the latest commit. This command does not remove the file from the staging area, and it does not create a commit.
To unstage a file without discarding its edits, use git restore --staged path/to/file. The edits remain in your working copy, so you can revise them or stage them again. If a file has both staged and unstaged edits, this command leaves the working copy alone; check git diff afterward to see what remains.
To discard both staged and unstaged changes to one tracked file and return it to the latest commit, use git restore --source=HEAD --staged --worktree path/to/file. Treat that as destructive: Git will replace your local edits to that file. Restore does not remove untracked files, because Git has no recorded version of them to restore. If an unwanted file is untracked, review it and remove it separately.
Use reset when you need to move a local commit
Reset moves your current branch to a different commit. It can also change what is staged and what is in your working copy. For example, git reset --soft HEAD~1 moves the branch back one commit while leaving the changes from that commit staged. This is useful if you just committed locally and want to adjust the commit before making it again.
With git reset HEAD~1, Git uses its default mixed mode: it moves the branch back one commit and unstages the resulting changes, while keeping them in your working copy. You can edit the files and stage only what you want. By contrast, git reset --hard HEAD~1 also replaces tracked working-copy files and discards the affected local edits. Check git status and the commit you are targeting before using --hard; do not use it as a routine cleanup shortcut.
These examples assume HEAD~1 is the commit you want to return to. Reset rewrites the position of your branch, so it is generally best for commits that have not been shared. If others may already have the commit, consider revert instead.
Use revert to undo a commit without removing it from history
Run git revert HEAD to create a new commit that attempts to reverse the changes introduced by the latest commit. The original commit remains in the history. This makes revert a useful choice when the commit has been shared, or when you want a clear record that a change was undone. To target a different commit, find its identifier with git log and use git revert followed by that identifier.
Revert may stop for conflicts if later changes overlap with the change you are reversing. Read Git’s message, resolve the affected files, and follow the instructions it provides to continue or abort. Check git status before starting; a clean working tree makes the result easier to understand. Reverting a merge commit requires an additional choice about which parent to keep, so review that case separately rather than applying the simple example blindly.
Choose by asking what you want to keep
For an unstaged mistake in a tracked file, restore can discard the edit. For a staged mistake you still want to work on, restore --staged takes it out of the staging area without deleting it. For a recent local commit you want to redo, reset --soft or the default mixed reset lets you keep the changes in different states. For a commit whose history you want to preserve, revert creates a new undoing commit.
After any of these commands, run git status and inspect the relevant diff. Undoing one layer of Git’s history can reveal changes at another layer, particularly when a file has both staged and unstaged edits. Checking the result before making another change helps you avoid undoing more than you intended.



