-1

So, I have a modicum of experience with git. So I've been using GitKraken to utilize the features of git. I believe I've screwed up somewhere, and lost weeks worth of work. I staged some files and did a commit, only to realize, I didn't want to stage all the files in a single commit. So what I did was right click on the commit and it was either, revert changes, or delete commit or something of that sort. So I did that, figuring I was just modifying the git repo. I then checked out my master branch without the current files. Now I'm not exactly sure which operation deleted my files but they're missing now. I thought checking out a branch doesn't mess with untracked files, so I thought it was a safe operation...

HELP! ;_; Where do I even start to figure out how to recover these missing files?

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
BAM5
  • 341
  • 2
  • 6

1 Answers1

2

If the changes were committed, but then you reverted that commit, then a revert commit will have been created which contains the changes which remove all the files. If this is the case, you can first try typing git log to view the history of commits behind the commit at the head of the master branch. Everything in this list can be checked out to go back to that revision by running git checkout a1b2c3d4..., where a1b2c3d4... is the hexadecimal hash you see of the commit. Another way of going back to the previous commit in your branch without looking through the log is to just type git checkout @~ or git checkout HEAD~.

If you can't find anything in the branch's log, you probably haven't created a revert commit, and have somehow reset the branch in a different way, a possible solution is to use git reflog. This should show a list of hexadecimal hashes of commits which you can checkout in the same way you would as mentioned previously. The reflog contains a list of every commit you have had checked out in your local working tree, even if they are across different branches or are no longer accessible from any branches after making a mistake with git reset.

If you have found a commit containing everything you want, take note of its hexadecimal hash, then checkout master again with git checkout master, and then run git reset --hard a1b2c3d4..., where a1b2c3d4... is the hash of the commit you want, which will bring master back to that state.

If you are unable to find a commit containing all your files in the commit log or the reflog, you may unfortunately not be able to recover your files using git, which will be the case if you never actually made a commit, and GitKraken cleaned and deleted the staged changes from your working tree completely. At this point you will have to look for recovery options elsewhere, such as:

  • Backups
  • Your text editor / IDE's swap files or file history
  • File recovery software run against your filesystem, as though you have just deleted some files normally without using git and without any backups
Candy Gumdrop
  • 2,745
  • 1
  • 14
  • 16