Git: Restore disappeared files that were added but not committed

277 views Asked by At

I started using git four weeks ago to synchronize files between two computers. The last two weeks I did not commit any changes. Yesterday I wanted to add a third computer. In a hurry, I ran some commands on Computer 1 to create a new branch (PreAlgo) and commit the changes of the last two weeks. In parallel on Computer 3, I tried to fetch the current state of the repo.

Unfortunately, all files I added on Computer 1 disappeared. I could recover all my git commands and a little of the responses from Matlab history from both computers. But as I worked in parallel on both, I can not recover the order of the commands between both computers.

I know the code below is an example of how to not use git. The initial branch of Computer 1 was AddFinEval. I am aware that I did a lot of mistakes. Hints for recovering the files are much appreciated.

Commands Computer 1

>> ! git branch PreAlgo
>> ! git commit -m "PreAlgo" --all
>> ! git push origin PreAlgo
>> ! git add PredictAvailability.m    % this file did not diasappear
>> ! git commit -m "PreAlgo" --all
>> ! git push origin PreAlgo
>> ! git add AlgorithmPlayground\     % files from this path disappeared
>> ! git add Predictions              % files from this path disappeared
>> ! git commit -m "PreAlgo" Initialisation.m
>> ! git push origin PreAlgo
>> ! git commit -m "PreAlgo" Initialisation.m
>> ! git push
>> ! git checkout PreAlgo
>> ! git commit -m "PreAlgo" --all
>> ! git checkout AddFinEval
Switched to branch 'AddFinEval'
>> ! git commit -m "PreAlgo" --all
Untracked files:
   (list of some untracked files that did not disappear)
nothing added to commit but untracked files present (use "git add" to track)
>> ! git push origin AddFinEval
To https://github.com/myname/myrepo

Commands Computer 3

>> ! git fetch --all
>> ! git reset --hard origin/master
>> ! git pull
>> ! git fetch --all
>> ! git reset --hard origin/master
>> ! git pull
>> ! git checkout PreAlgo
>> ! git fetch --all
>> ! git reset --hard origin/master
>> ! git checkout AddFinEval
>> ! git fetch --all
>> ! git reset --hard origin/master
>> ! git fetch --all
>> ! git reset --hard origin/AddFinEval

As proposed here How to restore files that were removed by git?, I tried

! git fsck --lost-found
! ls .git/lost-found/other

but the two files I found in lost-found contain only their title. However, in .git/objects/pack I found a 600MB .pack file created at about the time the files disappeared. One of the disappeared folders contained some large .mat files.

0

There are 0 answers