85

Is there a way to revert to a previous git commit in VS code?

I know I can see the changes between commits and the differences in the working tree but I want to know how to reset to the previous commit in visual studio code.

5 Answers 5

121

With the source control icon selected, if you then click the ellipsis ... at the top right.

With v1.48 you will see a list of options for submenus: under Commit is Undo Last Commit. Under Changes is Discard All Changes.

git submenus

4
  • 4
    but what that does is it even puts the changes that are now done to the previous commit . I want to reset back to the previous commit
    – Vikranth
    Oct 25, 2017 at 8:09
  • 7
    There is also a "Discard All Changes" - that would undo all changes since the last commit - is that what you want?
    – Mark
    Oct 25, 2017 at 14:08
  • ctrl + shift + p, type with 'discard', you can discard changes in single file or multiple files at once.
    – Kimmi
    Sep 4, 2019 at 20:29
  • 4
    what if the commit is already pushed? is there a way that we can removed the last push in VS code?
    – aj go
    Dec 24, 2020 at 7:47
37

Provided that you have GitLens extension installed you should be able to simply select "Restore" action from selected "File history" commit.

GitLens Restore file from File History

15

I use GitLens for that:

  1. In the source control tab, open File History.
  2. Choose your commit, then right-click Commit.
  3. Click on Revert Commit....

enter image description here

3
  • gitLens is an excellent product, it also shows the command in the terminal, so you can learn how to use it
    – MortenB
    Aug 18, 2021 at 13:45
  • Can you use this feature in free plan of GitLens?
    – MehmedB
    Nov 29, 2022 at 9:46
  • 1
    @MehmedB Only with public and local repositories. Jun 4 at 10:36
8

You can use this option in the Gitlens "commits" section to reset any number of previous (local) commits: just right click on the commit you would like to reset your current local branch

enter image description here

Then a list appears to allow you to choose a suitable reset option, as usual:

enter image description here

In recent vscode (1.51.1 currently installed) Gitlens extension has been integrated in the source control side view, by the way, instead of having its own button in the sidebar as before.

8

In vscode press Ctrl + Shift + P, type git undo, and hit Enter.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.