How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (2024)

How to Undo the last Git Commit in Visual Studio Code

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (1)

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (2)

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 filesthat were changed in the last commit have been returned to the staging area andthe commit has been removed.

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (3)

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (4)

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

# 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 VSCode:

  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.

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (5)

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (6)

# Losing or deleting an important file

If you happen to lose or delete an important file from your project by mistakeand can't seem to get it back, check out my other article:

VS Code has a native feature that tracks your local files and allows you torestore 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.

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (7)

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 inthe index and the working three.

shell

Copied!

git reset --soft HEAD~1

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (8)

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

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (9)

If you run the git commit command after running git reset --soft HEAD~1, youwould 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

Copied!

git reset --hard HEAD~1

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (10)

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

If you need to revert a Git commit in VS Code, use theGitLensextension.

  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.

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (11)

  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 deletinga commit from the commit history, it creates a new commit that inverses thechanges made with the commit.

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (12)

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (13)

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

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (14)

  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.

How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (15)

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

# Additional Resources

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

  • What is the .vscode folder and should you Commit it to Git
  • Changing the Git user or the GitHub account in VS Code
  • How to easily delete Git Branches in Visual Studio Code
  • VS Code: Git not found. Install it or configure it using the 'git.path' setting
  • Using VS Code as default Git editor, difftool and mergetool
  • How to Reset Visual Studio Code to the Default Settings
  • Please clean your repository working tree before checkout
  • How to rename a Directory or a File on the GitHub website
  • How to install a NuGet package in Visual Studio Code
  • How to highlight modified lines in Visual Studio Code
How to Undo the last Git Commit in Visual Studio Code | bobbyhadz (2024)

References

Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5868

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.