Ixxerja permezz ta’


Undo changes

TFS 2018

Visual Studio 2019 | Visual Studio 2022

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.
  • Amend your last commit to modify its content or message. For more information, see How to update your last commit.
  • 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.

Screenshot of the context menu options for staged files in Visual Studio.

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.

Screenshot of the context menu options for changed files in Visual Studio.

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.

  1. From the menu bar, choose Git > View Branch History to open the History tab for the current branch.

    Screenshot of the View Branch History option in the Git menu in Visual Studio.

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

    Screenshot of the Reset option in the context menu for a commit in the History window in Visual Studio.

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.

  1. From the menu bar, choose Git > View Branch History to open the History tab for the current branch.

    Screenshot of the View Branch History option in the Git menu in Visual Studio.

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

    Screenshot of the Revert option in the context menu for a commit in the History window in Visual Studio.

Next steps