0

I cloned my repository. Created a project out of the folder in to which I cloned. Made changes to the 5files, ran the tests. Everything is OK. Then I realized I did not created a branch and all my changes are in 'master' branch. I 'git add \path\to\file(s)', and I did not committed them. I PANICKED. I created a new branch 'X' by doing git checkout -b 'X'. NOT realizing, by switching thus, all the content ALREADY CAME from 'master' in to 'X', following a tip from the internet, I did git checkout master path/to/file(s) which were changed in master in order to bring those 5 files in to branch 'X'. Now realizing my mistake, I went in to 'master' to see my file changes. They are NOT there either in 'master' or 'X' branches!! When I ran 'git status' I see the following.`On branch master Your branch is ahead of 'origin/master' by 3065 commits. (use "git push" to publish your local commits)

` Where are the changes gone? How to recover them ? It is 4 days worth of work, sacrificing my thanksgiving holidays. Will some one please help me here? I am working in intelliJ. I am desperate. THANKS A LOT IN ADVANCE.

PraNuta
  • 629
  • 3
  • 12
  • 33
  • Does this answer your question? [Can I recover a branch after its deletion in Git?](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git) – FailingCoder Dec 03 '19 at 03:09
  • My question is different from another suggested answer by stackoverflow above, https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git in that, I did not delete my branch. I just added the files, not even committed them. – PraNuta Dec 03 '19 at 03:44
  • 1
    Remember : before checkout the master branch you have to stash your changes. And you have not done this Now, If you can see the changes in your IDE then preserve it in your local storage. and create a fresh branch from master then push it. after doing this add the changes that you have preserved and just push the changes in the new branch. This will help in monitoring the changes in your branch. It's showing 3065 commits because it's a new branch from master. If you don't find your changes that means they are overridden by the content of newly created branch – Harsh Mishra Dec 03 '19 at 04:53
  • My issue is similar to https://stackoverflow.com/questions/21925761/git-checkout-b-newbranch-lost-changes-on-current-branch except I did not commit in any branch. Just added in the master only. – PraNuta Dec 03 '19 at 05:20
  • IntelliJ may have your work backed up (I don't know, I don't use it, but I have heard of it doing things like this). Or, if you're on (e.g.) a Mac and have Time Machine backups set up, you may be able to get your files back from there. Git has overwritten them, as you asked it to, so Git won't be able to help. – torek Dec 03 '19 at 06:50
  • Ah, I read quickly last night, and didn't see that you had run `git add` (for some reason that particular command is not displayed as code in your question). In any case see sbat's answer. – torek Dec 03 '19 at 16:38

1 Answers1

2

First off all, ZIP your entire project with the .git folder before you run any further git commands or try anything else.

Your easiest option might be to use intelliJ local history: https://www.jetbrains.com/help/idea/local-history.html

As far as I remember, it is enabled by default, and you should easily get back your deleted files: https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/

However, even with git only all is not lost yet. When you issued git add command you have created git blobs for these files. When you did git checkout master . they are no longer in git index, but before git does its garbage collection, they are still physically in the .git.

I tried to model your situation locally, and after updating my local index with the changes from the other branch, I see the following dangling blob that represents previously added file:

$ git fsck
Checking object directories: 100% (256/256), done.
dangling tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling blob f2734a2fe05bfe4293d5a408c7e2ce849cbc62b2

I did git fsck --lost-found, and in .git\lost-found\other\f2734a2fe05bfe4293d5a408c7e2ce849cbc62b2 file I indeed found the content of the updated test file.

You can find more information on how git stores stuff internally in the Git Internals - Plumbing and Porcelain chapter of the free Pro Git book. It also provides a number of useful low-level commands that can help you in case of emergency like that.

To make sure this never happens again, I suggest Git Tools - Reset Demystified chapter. It explains the exact behavior of git checkout and git reset with different command line options, with/without path, with/without branch.

sbat
  • 1,453
  • 10
  • 21
  • Since, I started using intelliJ only this project alone, my fellow coworker tried the local history and could not find anything there, probably due to running the internet tip I mentioned **git checkout master path/to/file(s) which were changed in master** Anyways, I used another workspace, started the whole new development once again. Hopefully it won't take that long as the first time. I also learnt the lesson to either stash or commit, before changing the branch. – PraNuta Dec 04 '19 at 18:00
  • Good luck! :) I would be very interested though to see if `git fsck --lost-found` could recover any added files for you. – sbat Dec 04 '19 at 20:34
  • 1
    I did not try that @sbat. I just utilized the intelliJ project history feature alone. – PraNuta Dec 04 '19 at 20:54