7

I have quite a few changes that I would like to commit to new a branch but I've accidentally been working on the master in VS Code. This wouldn't have been an issue until I have accidentally clicked "Undo Last Commit" (twice, not once) because I almost published to master.

I am now especially worried that my progress will be overwritten when trying to commit my changes because VS Code tells me it would require me to first "pull." (I assume overwriting my files).

Sorry if I am under-informed on my situation, but the thought of losing my progress scares me too much to just start "trying" things as I always do (that's what got me here in the first place).

Drei
  • 669
  • 1
  • 6
  • 22
  • 1
    I recommend getting a good Git GUI like Gitkraken, which has a view that shows all branches simultaneously which makes resets and reverts much easier than how Visual Studio shows branch history. – Dai Apr 30 '18 at 01:27
  • 1
    @Dai Thanks, GitKraken I installed it and it has already helped me with something else! It also looks like they are working on a VS Code Plugin too (currently closed beta). – Drei Apr 30 '18 at 01:34

2 Answers2

12

The answer is really quite simple after looking into what "undo" actually does.

VS Code "Undo Last Commit" simply runs git reset HEAD~. These actions are in the output if you open up the "GIT" tab. The solution however can be found by looking at what was recorded by running git reflog and then manually running the git reset to before VS Code ran it. here is a better reference.

(I'll also leave this up in case someone else has the same worrisome mistake in VS Code)

Drei
  • 669
  • 1
  • 6
  • 22
  • 1
    Mostly the command executed in the background will have the `--soft` flag like this: `git reset --soft HEAD~` – Abdul Rahman Kayali Aug 10 '21 at 10:06
  • For reference: Relevant Function: https://github.com/microsoft/vscode/blob/8e65ae28d5fb8b3c931135da1a41edb9c80ae46f/extensions/git/src/commands.ts#L1641 Relevant line: https://github.com/microsoft/vscode/blob/8e65ae28d5fb8b3c931135da1a41edb9c80ae46f/extensions/git/src/commands.ts#L1661 – c-shubh Sep 10 '21 at 19:37
6
  1. go to three dots (...) on the upper right corner of the vscode
  2. go to commit
  3. select undo last commit

See below image:
enter image description here

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Ashish Singh
  • 97
  • 1
  • 3