How to Undo the last Git Commit in Visual Studio Code
# Table of Contents
- How to Undo the last Git Commit in Visual Studio Code
- Undo the most recent Git commit in VS Code using the Command Palette
- Losing or deleting an important file
- Undo the last git commit in VS Code using commands
- 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:
- Click on the Source Control icon in the Activity bar to the left.
- Click on the three dots (ellipsis
...
) icon at the top of the sidebar,hover over Commit and select Undo Last Commit.
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.
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:
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type discard and select your preferred command.
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:
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type undo commit and select Git: Undo Last Commit.
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.
# 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:
- Press:
Ctrl
+Shift
+P
on Windows and Linux.Command
+Shift
+P
on macOS.
Note: you can also press F1
to open the Command Palette.
- Type toggle terminal and select View: Toggle 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 inthe index and the working three.
shell
Copied!
git reset --soft HEAD~1
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.
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
# Revert or Undo a Git commit using GitLens in VS Code
If you need to revert a Git commit in VS Code, use theGitLensextension.
- 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.
- Type GitLens.
- Make sure you have the extension installed.
You can then:
Click on the Source Control icon and expand the COMMITS menu.
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.
If you need to restore your changes based on a specific commit:
Click on the Source Control icon and expand the File History menu.
Right-click on a commit and select Restore (Checkout).
If you need to reset your current branch to a specific commit:
Click on the Source Control icon and expand the COMMITS menu.
Right-click on a specific commit and select Reset Current Branch toCommit.
- 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.
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