When you want to undo changes in a Git repo, first decide what type of changes you want to undo. For example, you might want to:
Discard uncommitted changes to a file by reverting the file to its last committed version. You can also revert a file to any committed version.
Revert a branch to a prior state by resetting it to a previous commit.
Undo the changes made by a shared commit by creating a new commit that reverses the changes. Because this approach won't rewrite existing commit history, it's suitable for reversing changes made by commits that were pushed and are in use by others.
Fix a problem introduced in a prior commit by creating a new commit that includes the fix. For more information, see How to create a commit.
In this article you learn how to:
Discard uncommitted changes to a file
Revert a branch to a prior state
Undo the changes made by a shared commit
Discard uncommitted changes to a file
If you've made changes to a file but haven't committed those changes, you can use Git checkout to quickly discard the changes and revert the file to its last committed version.
Git checkout can also revert a file to any committed version, when you specify the commit ID.
Warning
If there's a chance you might want to reapply the changes that Git checkout would discard, consider stashing those changes instead.
Visual Studio 2022 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer. Visual Studio 2019 version 16.8 also offers the Team Explorer Git user interface. For more information, see the Visual Studio 2019 - Team Explorer tab.
Visual Studio supports discarding uncommitted changes to a file by reverting the file to its last committed version.
In the Git Changes window, identify the file with the changes that you want to discard. If the file is in the Staged Changes section, right-click it and choose Unstage. Unstaged files show up in the Changes section.
If the file is in the Changes section, right-click it and choose Undo Changes to discard all changes to the file since the last commit.
Visual Studio 2019 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer.
Visual Studio supports discarding uncommitted changes to a file by reverting the file to its last committed version.
In the Git Changes window, identify the file with the changes that you want to discard. If the file is in the Staged Changes section, right-click it and choose Unstage. Unstaged files show up in the Changes section.
If the file is in the Changes section, right-click it and choose Undo Changes to discard all changes to the file since the last commit.
Visual Studio supports discarding uncommitted changes to a file by reverting the file to its last committed version.
In the Changes view of Team Explorer, identify the file with the changes that you want to discard. If the file is in the Staged Changes section, right-click it and choose Unstage. Unstaged files show up in the Changes section
If the file is in the Changes section, right-click it and choose Undo Changes to discard all changes to the file since the last commit.
You can use the Git checkout command to discard uncommitted changes to a file by reverting the file to its last committed version:
git checkout <file>
For example, git checkout README.md discards uncommitted changes to the README.md file.
Git checkout also supports reverting a file to a previously committed version when you specify a partial or full commit ID that uniquely identifies a commit:
git checkout <commit ID> <file>
For more information about finding a commit ID, see Find a commit ID.
Note
The use of the Git checkout command to revert a file differs from its typical use, which is to switch between branches. The checkout command output tells you whether it's switching between branches or updating a file, and warns if it's unclear which one you're trying to do.
Revert a branch to a prior state
You can revert a branch to a prior state by using Git reset to reset the branch to a previous commit. Git reset affects all files in all branch folders.
Git reset has a few options. The default option is to revert the branch to a previous commit, but retain all the subsequent changes as uncommitted changes. Another option is to revert a branch to a previous commit by discarding all changes to all branch files since that commit.
Warning
Don't reset a branch to a commit prior to the last commit if the last commit has been pushed and shared with others. Doing so will result in your local branch history no longer matching the remote branch history. For shared branches, see Undo the changes made by a shared commit.
From the menu bar, choose Git > View Branch History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to reset, and then choose Reset > Delete Changes (--hard) to reset the branch to the selected commit and delete all changes to all branch files since that commit. Or, choose Reset > Keep Changes (--mixed) to reset the branch to the selected commit and retain all subsequent changes as unstaged changes.
From the menu bar, choose Git > View Branch History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to reset, and then choose Reset > Delete Changes (--hard) to reset the branch to the selected commit and delete all changes to all branch files since that commit. Or, choose Reset > Keep Changes (--mixed) to reset the branch to the selected commit and retain all subsequent changes as unstaged changes.
In the Changes view of Team Explorer, choose Actions > View History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to reset, and then choose Reset > Delete Changes (--hard) to reset the branch to the selected commit and delete all changes to all branch files since that commit. Or, choose Reset > Keep Changes (--mixed) to reset the branch to the selected commit and retain all subsequent changes as unstaged changes.
You can use Git reset to revert a branch to its state in a previous commit. The Git reset command can be used with various flags:
The --hard flag tells Git to reset the branch to the specified commit, and to discard all subsequent changes. Be careful with this option since you can lose work using it.
The --mixed flag tells Git to reset the branch to the specified commit, but to keep all subsequent changes as unstaged changes. This option is the default.
The --soft flag tells Git to reset the branch to the specified commit, but to keep all subsequent changes as staged and unstaged changes per their previous state.
A common use of Git reset is with the --hard option to discard all uncommitted changes in all files since the last commit:
git reset --hard
To hard reset a branch to a different commit than the last commit, specify the commit ID or a partial commit ID that uniquely identifies the commit:
git reset --hard <commit ID>
For more information about finding a commit ID, see Find a commit ID.
Undo the changes made by a shared commit
You can undo the changes made by a commit by using Git revert to create a new commit that reverses those changes. Git revert doesn't delete the original commit. This approach is suitable for undoing the changes made by a shared commit because Git revert won't alter the previous commit history, and so local and remote branch histories continue to match.
From the menu bar, choose Git > View Branch History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to revert and choose Revert to create a new commit that undoes the changes made by the selected commit.
From the menu bar, choose Git > View Branch History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to revert and choose Revert to create a new commit that undoes the changes made by the selected commit.
In the Changes view of Team Explorer, choose Actions > View History to open the History tab for the current branch.
In the History tab for the current branch, right-click the commit you want to revert and choose Revert to create a new commit that undoes the changes made by the selected commit.
You can use the Git revert command to undo the changes made by a commit and create a new commit with the reversed changes. The commit whose changes were reversed remains in Git history.
git revert <commit ID>
If the revert operation was successful, Git will open an editor window that prompts you to enter a commit message for the new commit.
Git will notify you if there are conflicts during the revert operation. You can either resolve the conflicts and then run git revert --continue, or run git revert --abort to undo the revert operation.
Find a commit ID
You can use the Git log command to get the ID of a commit. The --oneline flag abbreviates the output.
git log --oneline <branch>
The Git log command lists the most recent commits first, starting with the commit at the tip of the specified branch (the current branch if unspecified) and then iterates backward through ancestor commits. Each commit ID in the abbreviated output is a partial SHA-1 checksum that uniquely identifies the commit. For example, git log --oneline main might output:
e745d06 (HEAD -> main) Add a test initialization class
31da50b Add network switch test
0c14391 Add readme file
32e3946 Add project files.