1

I just made a huge mistake with Git and I'm afraid I've just lost 2 days of work, am hoping one of you might be able able to help me undo that!

I'm new to Git, so I'm constantly looking online how to do certain things, a few days ago I created a new branch with the web interface, since I've worked and worked without committing or pushing (or checking out said branch) since, today I thought it was about time I pushed my work, so I did the following;

git add .

git commit -m "these changes were awesome"

Then before git pushing, I thought "I really should put this on a new branch" a quick Google search on "how to change branch" came up with "git checkout"... "ok great!" I thought, and like the ape I am did it without a second thought, to see my file structure in VS Code massively change... then I realised I had lost it all.

Is there any hope that Git stores these deleted files anywhere? Is there any hope that I can get these files back and not have to jump out my window?

halfer
  • 19,824
  • 17
  • 99
  • 186
beau c
  • 93
  • 1
  • 9

2 Answers2

2

You added all your files, then committed them, so sounds like they're safely stored on your branch. How are you interacting with git (e.g. shell, git extensions, etc...)?

Run "git branch" if you're in the console. What do you see? If in git extensions or similar, what branches / commits do you see?

If you switched branches, you should be able to checkout the previous branch to see your committed work. Otherwise (if on current branch) carefully reset or stash changes on the current branch if you see your commit in the previous log ("git log" command).

Daniel Mohr
  • 654
  • 7
  • 7
  • Thanks for your response, I'm using shell. – beau c Dec 11 '17 at 16:59
  • I just tried to do git checkout master, as i was on master, but it put it back to the way the pushed version of master is, and not the version i commited – beau c Dec 11 '17 at 17:00
  • There's details here on git checkout for background. https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout – Daniel Mohr Dec 11 '17 at 17:00
  • run "git log", what do you see? Press "q" when you're done to quit / exit the log view. – Daniel Mohr Dec 11 '17 at 17:00
  • Ahh relief! running a log, then a reset allowed me to get it back, thanks for your help, I'll post an answer detailing the exact commands I used to do this for others in my case too. Your link helped – beau c Dec 11 '17 at 17:07
  • Glad to hear! Any more questions, let me know. Nothing like a gitastrophe to strike fear into the heart of the bravest developer. – Daniel Mohr Dec 11 '17 at 17:10
  • @beauc At least you committed your work. That way it's safe in git's hands. – evolutionxbox Dec 11 '17 at 17:12
0

I've marked @Daniiel Mohr's answer as correct because he helped me get there, but I'll post here the exact steps I used for others in my position,

git reflog

This gives you a log with your recent activity, look for your commit in a list like this:

f2ff96e HEAD@{3}: checkout: moving from multi_content_support to dynamic-contents
b6ebc14 HEAD@{4}: commit: FixedContainer and MutableContainer added with dynamic support, Image, Label, Button and TextInputField upgraded to dynamic

Find the id of your commit, in my case "b6ebc14"

Then use git reset with your id:

git reset --hard b6ebc14

et voilà, git will kindly stop your anxiety attack ;)

good luck!

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
beau c
  • 93
  • 1
  • 9
  • 1
    git reflo**g** (t -> g). – phd Dec 11 '17 at 17:14
  • I'm afraid by using `git reset --hard` you might have done more harm than what you thought you had initially done. If I understand correctly, you only switched from one branch to another branch (not doing any harm at all), and could have just switched back by using `git checkout` a second time. – mkrieger1 Dec 12 '17 at 01:31
  • You might be right, the git checkout did remove all the work I had done, but it must have been safely stored on the branch I was working in, though I tried to swap back to that branch with no luck. But reading my reflog after, I didn't swap back to the right branch anyway... I messed up on so many levels in the stress of possibly losing everything! But the reset --hard did no damage. Though I see how it could have – beau c Dec 15 '17 at 14:21