Undo changes
TFS 2017 | TFS 2015 | TFS 2013
Visual Studio 2019 | Visual Studio 2017 | Visual Studio 2015
When undoing changes in Git, first decide what type of changes you are looking to undo. These changes fall into three categories:
- Discard uncommitted changes to a file, bringing the file back to the version in the last commit.
- Reset your local branch to a previous commit.
- Revert changes pushed to a remote branch and shared with others.
If you just need to make small updates such as to fix a typo or small problem introduced in your last commit, consider amending your previous commit or fixing the change in a new commit instead of any of these other steps.
In this tutorial you learn how to:
- Discard uncommitted changes to a single file
- Revert changes in shared commits
- Reset a branch to a previous state
Discard uncommitted changes to a single file
Restore file contents back to a known good version, removing unwanted changes.
Warning
These commands will overwrite your existing file changes. If you think you might want these changes later, consider stashing them instead.
Note
Visual Studio 2019 version 16.8 and later versions provide a new Git menu for managing the Git workflow with less context switching than Team Explorer. Procedures provided in this article under the Visual Studio 2019 tab provide information for using the Git experience as well as Team Explorer. To learn more, see Side-by-side comparison of Git and Team Explorer.
Visual Studio 2015 & 2017
Open up the Changes view in Team Explorer.
Under the Changes section, find the file that you want to restore to the previous version. If your change is staged, remove it from the Staged Changes section by right-clicking and selecting Unstage.
Right-click that file and select Undo Changes.
Revert changes in shared commits
Use revert
to undo the changes made in your commits pushed to shared branches. The revert
command creates a new commit that undoes the changes on a previous commit. No history is rewritten
in a revert
, making it safe to use when working with others.
Note
Visual Studio 2019 version 16.8 and later versions provide a new Git menu for managing the Git workflow with less context switching than Team Explorer. Procedures provided in this article under the Visual Studio 2019 tab provide information for using the Git experience as well as Team Explorer. To learn more, see Side-by-side comparison of Git and Team Explorer.
Open up the Changes view in Team Explorer. Select Actions and choose View History from the drop-down. In the history window that appears, right-click the commit to undo and select Revert from the context menu.
Reset a branch to a previous state
Use reset
to bring a branch in your local repository back to the contents of a previous commit. The most common use of the reset
command is
to simply discard all changed files since the last commit and return the files to the state they were in at the most recent commit.
Warning
Don't use reset
on branches shared with others. Use revert instead.
Open up the Changes view in Team Explorer.
Select Actions and choose View History from the drop-down.
In the history window that appears, right-click the commit to reset the repo to and select Reset from the context menu.
Choose Reset and delete changes....
A reset
affects all files in the current branch on the repository, not just those in your current directory. Reset
only discards changes that haven't
been committed yet.