6

I did git checkout -- . mistakenly .

changes neither committed nor stashed.. is their a way to retrieve my changes?

tried git reflog changes wasn't reflected in it..

if i commit or stash i know to retrieve but what about checkout without doing either?

things i did,

git status

after i noticed some useless changes so i tried to do checkout changes instead i did git checkout -- .

so is it possible to retrieve?

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • Before doing anything else, check if you had opened some of the modified files in an editor. Maybe the editor has the old contents still buffered and you can restore them that way. – mkrieger1 Oct 10 '17 at 12:36
  • 2
    Possible duplicate of [Accidentally reverted to master, lost uncommitted changes](https://stackoverflow.com/questions/7147680/accidentally-reverted-to-master-lost-uncommitted-changes) – mkrieger1 Oct 10 '17 at 12:37

6 Answers6

8

You cannot undo such operation. The reflog is a great tool, but records only what happens to refs (branch, tag, HEAD, ...), and here you did not modify any ref.

By typing git checkout -- ., you discarded any unstaged changes, so Git was never aware of these changes.

Your backup system is your only hope here (using Git doesn't mean one shouldn't do backups, too).

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
4

If you use Android Studio, it has Local History when you right click the mouse.

Local History Local commit

Francis Bacon
  • 4,080
  • 1
  • 37
  • 48
4

In your IDE (if you haven't closed it since) try using undo. For Visual Studios the undo is (ctrl + z).

1

Generally i use Netbeans IDE for programming and it has Rich features for helping developer,

so it has feature named History which stores every changes of that file whenever you save the file it saves it as history.

And it also saves all the commits of git, so it stores all the history from the day you have added project to netbeans to till now,

And additionally you can also compare your historical version of file to current version of file, it is very useful feature.

You can see in below image there is two option under opened file

1.Source(in it you can see and edit current file here)

2.History (in which you can check all the older version of file.)

enter image description here

You can see i have Books.php file under it in history tab, there is list of all the older version here from git commit, you can check all of it and also compare it to current file.

So if you have mistakenly done git checkout then you can check that file's history and you can undo the changes or get back your lost changes.

Haritsinh Gohil
  • 5,818
  • 48
  • 50
1

Depending on the IDE you used to modify the files in the first place, they might have a backup. For instance in Vim, filename.ext will have a backup named filename.ext~.

simlmx
  • 999
  • 12
  • 17
-1

Simple! I was also facing same tension for a few minutes. but I found the solution, which is When I run checkout, git shows me a warning that is

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  617bd25 my commit message

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 617bd25

Switched to branch 'master'

So i simply run the suggested command

git branch branch-name 617bd25 
git checkout branch-name

It solve my problem

Mudasir Habib
  • 704
  • 7
  • 14