4

Commands I used:

  1. git add . (after added I reset them all)
  2. git reset .
  3. git checkout .

after I checked out them all I understand I need them. Is there any way to get changes back?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
userPlus
  • 319
  • 3
  • 9
  • Check out https://stackoverflow.com/questions/7147680/accidentally-reverted-to-master-lost-uncommitted-changes and https://stackoverflow.com/questions/11094968/in-git-how-can-i-recover-a-staged-file-that-was-reverted-prior-to-committing it is potentially possible. – Chris Aug 03 '17 at 14:33

2 Answers2

4

If your files were staged like you said they were, you may be able to get them back using the git fsck command with the --lost-found option.

If you run git fsck --lost-found you will see something like this (with potentially more dangling blob lines):

Checking object directories: 100% (256/256), done.
dangling blob 84eab6f56e81cebe1356c9c2a6e2882c05f5fc01

Some of these dangling blobs could be your missing files.

If you run git show <SHA of a dangling blob> you will be able to see the contents of that file, unfortunately the file names are likely lost.

However you can copy the output back into the appropriate files. Alternatively after running git fsck --lost-found the dangling blobs will be saved to the .git/lost-found/other/ directory in the root of your repository.

Jack Gore
  • 3,874
  • 1
  • 23
  • 32
0

Yes, if you've staged your changes then you can absolutely get your files back. When you run git add, files are actually added to Git's object database. At the moment that you do, git will put the file in the index:

% git add bar.txt
% git ls-files --stage
100644 ce013625030ba8dba906f756967f9e9ca394464a 0   bar.txt
100644 6af0abcdfc7822d5f87315af1bb3367484ee3c0c 0   foo.txt

Note that the entry for bar.txt contains the object ID of the file. Git has actually added the file to its object database. In this case, Git has added it to the repository as a loose object:

% ls -Flas .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
4 -r--r--r--  1 ethomson  staff  21 14 Jun 23:58 .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a

These files will eventually be garbage collected. But this will happen in a matter of months, not days. Until these files are garbage collected, you can recover them.

The easiest way to do this is to download and install the git-recover program in interactive mode:

% git recover -i
Recoverable orphaned git blobs:

61c2562a7b851b69596f0bcad1d8f54c400be977  (Thu 15 Jun 2017 12:20:22 CEST)
> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
> veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
> commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

Recover this file? [y,n,v,f,q,?]: 

git-recover looks for files in the object database that are not committed (or in the index). You can find out more about git-recover in the blog post announcing it.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187