-1

I'm new to visual studio code and I accidentally reverted back to my previous git commit. All the changes I had to my files seem to be gone. Is there a way to get them back?

torek
  • 448,244
  • 59
  • 642
  • 775
Penguin
  • 1,923
  • 3
  • 21
  • 51
  • 1
    Do you actually mean a `git revert`, or did you `reset` uncommitted changes? In the former case, see `git reflog`, in the latter case they're gone. – jonrsharpe Jul 29 '21 at 15:00
  • I'm not sure to be honest. A pop up window came up and after closing it it just went back to the last commit – Penguin Jul 29 '21 at 15:01
  • 1
    git checkout master ? or git checkout -b "the commit you want" – Achille G Jul 29 '21 at 15:16
  • Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – Adrian W Jul 29 '21 at 20:54
  • As far as the Git side of things go, you need to find out what Visual Studio Code actually did. Git *may* be able to get stuff back, or may not. I added a [tag:visual-studio-code] tag since that's where your problem needs to start: not with Git at all. – torek Jul 30 '21 at 08:05
  • @torek I appreciate that. I thought I added that tag, weird. But yea, I get it. Still trying to figure out what happened – Penguin Jul 30 '21 at 14:08

3 Answers3

1

Probably you have run the

git reset --hard HEAD~1

If you have committed your changes, you can retrieve it back, but if you haven't done the commit, then I don't think so.

Using below command, you will get the history of changes you have done in your repository.

git reflog

When you see the commit in the reflog, you can either cherry-pick it to your current reverted HEAD.

git cherry-pick <commit_hash>

This will put the commit you reverted on top of your current HEAD. Note that the commit hash will change when you cherry pick

Also, you can give a try for below command also. I haven't tested it, but I guess this will also work.

git reset --hard <commit_hash_of_reverted_commit_you_see_in_reflog>

Above command should reset your current branch to the commit you mentioned. This is generally used when you want to reset your branch to and old HEAD~x commit or reset your branch to the state in origin (git reset --hard origin/<branch_name>, after a git fetch)

Ron Thomas
  • 737
  • 1
  • 8
  • 20
1

git revert creates a new commit reverting the changes made in a previous commit. It is perfectly acceptable to do a git revert on the revert commit to bring those changes back.

Adam
  • 772
  • 3
  • 10
0

use

git log

it will show you the commit details and you just have to use this commit id in order to get it back into your code and cancel the revert. and please always push to have a safe version somewhere else than your computer

  • This just shows me the commits. I made changes after the last commit. And yea, I forgot this one time – Penguin Jul 29 '21 at 15:02