How to Undo the last Git Commit in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
5 min

banner

# Table of Contents

  1. How to Undo the last Git Commit in Visual Studio Code
  2. Undo the most recent Git commit in VS Code using the Command Palette
  3. Losing or deleting an important file
  4. Undo the last git commit in VS Code using commands
  5. Revert or Undo a Git commit using GitLens in VS Code

# How to Undo the last Git Commit in Visual Studio Code

To undo the last git commit in Visual Studio Code:

  1. Click on the Source Control icon in the Activity bar to the left.

click source control

  1. Click on the three dots (ellipsis ...) icon at the top of the sidebar, hover over Commit and select Undo Last Commit.

undo last commit using ui

The command brings your branch to the state right before you made the commit.

If you click on the Undo Last Commit option, you will see that the files that were changed in the last commit have been returned to the staging area and the commit has been removed.

files returned to staging area

If you were to commit your files at this point, you would redo the same commit.

In some cases, you might want to discard the changes that you've made in the previous commit.

If that's the case:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type discard and select your preferred command.

discard changes after reverting commit

The Git: Discard all Changes command undoes all changes since the last commit.

# Undo the most recent Git commit in VS Code using the Command Palette

You can also use the Command Palette to undo the most recent Git commit in VS Code:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type undo commit and select Git: Undo Last Commit.

type undo commit

If you click on the Source Control icon in the Activity bar to the left, you will see that the commit has been undone and the changed files are now in the staging area.

files returned to staging area

# Losing or deleting an important file

If you happen to lose or delete an important file from your project by mistake and can't seem to get it back, check out my other article: view local history & restore previous file versions in VS Code

VS Code has a native feature that tracks your local files and allows you to restore previous versions.

# Undo the last git commit in VS Code using commands

You can also use git commands to undo the last commit in VS Code.

If you need to open the terminal in VS Code:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type toggle terminal and select View: Toggle Terminal.

vscode open terminal

You can also open the terminal by using the keyboard shortcut:

  • on Windows and Linux: press Ctrl + ` (backtick).
  • on macOS: press Ctrl + ` (backtick).

# Using a soft reset

The following command undoes the most recent commit and leaves the changes in the index and the working three.

shell
git reset --soft HEAD~1

undo last commit soft reset

If you run the git status command you will see the same files you had prior to running Git commit.

The files that were changed in the commit are brought back to the staging area.

files are back in staging area

If you run the git commit command after running git reset --soft HEAD~1, you would redo the same commit.

# Using a hard reset

If you want to destroy the last commit and throw away any uncommitted changes, use a hard reset.

Note that this command is destructive. You will undo the last commit, however, all changed files will also be removed.
shell
git reset --hard HEAD~1

undo last commit using hard reset

# Revert or Undo a Git commit using GitLens in VS Code

If you need to revert a Git commit in VS Code, use the GitLens extension.

  1. Click on Extensions in the left sidebar.
  • You can also open the Extensions menu by pressing:
    • Ctrl + Shift + X on Windows or Linux.
    • Command + Shift + X on macOS.
  1. Type GitLens.

install gitlens extension

  1. Make sure you have the extension installed.

You can then:

  1. Click on the Source Control icon and expand the COMMITS menu.

  2. Right-click on the commit you want to revert and select Revert Commit....

The git revert command is a forward-moving undo operation. Instead of deleting a commit from the commit history, it creates a new commit that inverses the changes made with the commit.

The command is safe because you are still able to go back to the previous commit if you decide.

click revert commit

If you need to restore your changes based on a specific commit:

  1. Click on the Source Control icon and expand the File History menu.

  2. Right-click on a commit and select Restore (Checkout).

file history restore checkout

If you need to reset your current branch to a specific commit:

  1. Click on the Source Control icon and expand the COMMITS menu.

  2. Right-click on a specific commit and select Reset Current Branch to Commit.

file history restore checkout

  1. You have to select 1 of 3 options:
  • Reset - leaves changes in the working tree.
  • Soft Reset - leaves changes in the index and the working three.
  • Hard Reset - discards all changes.

select if soft or hard reset

I've also written a detailed article on how to view the Git history in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.