I accidentally deleted .git/index
, is there a way to recover it? It's permanently deleted. I haven't committed anything yet.
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.
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.
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.
To rebuild the index file, you can try these two commands:
git reset # re-scan the working directory git add -u # update the index
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With