Undo changes

Azure Repos | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015 | VS 2017 | VS 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 now includes a new Git tool that provides an improved experience when connecting to a Git repository. When you enable this tool, the Team Explorer tool is effectively disabled when connected to a Git repository. You can acquire the new tool by downloading Visual Studio 2019 version 16.6. To enable and use the new tool, see Git experience in Visual Studio (Preview).

Visual Studio 2015 & 2017

  1. Open up the Changes view in Team Explorer.

  2. 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.

  3. Right-click that file and select Undo Changes.

    Reset a single file with Git in Visual Studio

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.

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.

Revert changes from Visual Studio.

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.

  1. Open up the Changes view in Team Explorer.

  2. Select Actions and choose View History from the drop-down.

  3. In the history window that appears, right-click the commit to reset the repo to and select Reset from the context menu.

  4. Choose Reset and delete changes....

    Reset a branch from Visual Studio

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.

Next steps