How can I retrieve the local changed files which I wrongly reset in git

350 views Asked by At

Below is the situation which explains what's the matter about git.

  1. I was using the 'develop' branch and already made several changes to the local files such as .sql, .java, .js...

  2. I made a local branch called 'develop_some_future' since my boss wants to confirm my changes before merging main 'develop' repository.

  3. Apparently my local file changes @'develop' branch have applied to 'develop_some_future' branch and I started editing local files again.

  4. For some reason, I tried to pull the files that my co-workers already committed, but it has failed(Probably I couldn't set 'develop_some_future' branch well). So, I changed current branch to 'develop' branch and tried to pull them.

  5. Fortunately it got worked, and then I tried to back to the 'develop_some_future' branch.

  6. A dialog pops up suddenly while changing the branch and asked me that 'your local change for "~~~.sql" would be deleted since it's not committed yet. To avoid this, please commit that file or choose reset.'

  7. Because I thought only '~~~.sql' would be changed to the latest committed state and that was not a problem, I selected 'reset' button, but unfortunately all local changes have gone.

Anyone knows how can I retrieve the date before reset?

I found that both 'git reflog' and 'git reset HEAD{}' commands are useful. However, git reflog shows only commit, merge, and checkout changes and so I can't find reset status at all.

1

There are 1 answers

0
knittl On

I'm afraid your files are gone for good, unless you had previously staged the file somehow (add or stash save).

If it had been staged, a blob object was created by Git which is now dangling (git fsck will tell you dangling objects). You can then resurrect the file by writing the blob's content with git cat-file to a new file.

If not, your'e screwed, and you have to do your work again.


It seems the problem arose in the first place, because you thought that unstaged/uncommitted changes belong to the current branch. They do not, and only live in the working directory. They will be carried over when switching branches (unless, of course, the same file was modified in the branch. Git will then refuse to checkout the other branch).