Last updated: Apr 6, 2024
Reading time·5 min
To undo the last git commit in Visual Studio Code:
...
) 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 files that were changed in the last commit have been returned to the staging area and the 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 the previous commit.
If that's the case:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.The Git: Discard all Changes command undoes all changes since the last commit.
You can also use the Command Palette to undo the most recent Git commit in VS Code:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.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.
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.
You can also use git commands to undo the last commit in VS Code.
If you need to open the terminal in VS Code:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.You can also open the terminal by using the keyboard shortcut:
Ctrl
+ ` (backtick).Ctrl
+ ` (backtick).The following command undoes the most recent commit and leaves the changes in the index and the working three.
git reset --soft HEAD~1
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.
If you run the git commit
command after running git reset --soft HEAD~1
, you
would redo the same commit.
If you want to destroy the last commit and throw away any uncommitted changes, use a hard reset.
git reset --hard HEAD~1
If you need to revert a Git commit in VS Code, use the GitLens extension.
Ctrl
+ Shift
+ X
on Windows or Linux.Command
+ Shift
+ X
on macOS.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 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.
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 to Commit.
I've also written a detailed article on how to view the Git history in VS Code.
You can learn more about the related topics by checking out the following tutorials: