Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover `.git/index` locally?

I accidentally deleted .git/index, is there a way to recover it? It's permanently deleted. I haven't committed anything yet.

like image 803
dyarbrough Avatar asked Jul 08 '16 20:07

dyarbrough


People also ask

How do I restore index files?

2020, Git 2.23+: use the git restore command: You can specify the source (index), default is the index and destination (working tree). That will restore the working tree from the index. Yes, checkout can be destructive but it is a user command whereas checkout-index is really plumbing.

How do I restore a local git repository?

In the top right corner of GitHub.com, click your profile photo, then click Your organizations. Next to the organization, click Settings. In the left sidebar, click Deleted repositories. Next to the repository you want to restore, click Restore.

How do I fix a corrupted index in git?

To rebuild the index, you can simply do git reset --mixed . This makes the index look like the last commit while leaving the worktree alone. If you had any local changes you git add ed but did not commit yet, you will need to re-add those.


Video Answer


2 Answers

To rebuild the index file, you can try these two commands:

git reset         # re-scan the working directory git add -u        # update the index 
like image 102
kenorb Avatar answered Sep 26 '22 01:09

kenorb


You can recover the index as of the last checkout with git reset. Any content you had added since then is still added, it's in the repository, but the index was the only place that recorded the association of path and content. You can have git fsck drop unreachable objects into a lost'n'found directory, see its docs, then the quickest route over familiar territory is to just drop the content back in the worktree and add it again, git won't duplicate contents but it will recover the index entry.

like image 40
jthill Avatar answered Sep 22 '22 01:09

jthill