tnecniv
tnecniv

Reputation: 157

git staged file twice, can you recover the old version?

if i make changes to a file.txt

then git add file.txt

then i make more changed to file.txt

then i do git add file.txt again

i realize i made a mistake on my second changes/edit to the file. can you go back to the version of the file that i originally staged?

Upvotes: 1

Views: 192

Answers (2)

tnecniv
tnecniv

Reputation: 157

As J. Titus said in a comment:

You never committed to produce a reference, so no.

Upvotes: 1

ams
ams

Reputation: 25579

Yes, in theory the objects are still in .git/objects until the next time git gc is run (which happens automatically after certain other actions).

Unfortunately figuring out which object could be tricky. If the file was edited and added recently then the time stamp will help.

ls -lrt .git/objects/*/* | tail
-r--r--r-- 1 ams users     6976 Apr  6 19:53 .git/objects/0c/3546bd9101264cd6a9fb1fb54cea04c6e0a4b8

If you see a file with a likely timestamp then you can retrieve the contents like this:

git show 0c3546bd9101264cd6a9fb1fb54cea04c6e0a4b8

Upvotes: 5

Related Questions