2

I am new to Git and I think I removed files on my local accidentaly. I used this command git rm -r --cached . and it removed the files. But I didn't commit it yet.

How can I undo it?

1
  • Kudos for stopping and asking what to do next. Most questions here on Stack Overflow in such a situation seems to keep going with "I added all my changes, committed, then did a force-push because git wouldn't let me push without it, now my repository is broken, HALP!". You, instead, stopped, and asked. So kudos for that, you're on the right path. Commented Dec 31, 2017 at 22:46

2 Answers 2

2

git rm documentation is kind of clear:

--cached

Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

So if you lost files it's because there was already commits existing on the repository (not yours, but others ones).

Try to run git reset it should clear all your not committed actions, so you should find the repository like it was before this.

Sign up to request clarification or add additional context in comments.

2 Comments

pssst... loose /= lose
@evolutionxbox psst, thanks!
0

The index contains a path and an object id. git rm --cached erases the index entry, undoing the act means putting it back the way it was.

If the content was unchanged since checkout, git reset -- path/to/it is your ticket. If you git added the content, it's in the repository but the association between path and content is lost, you can find objects in .git/objects created about the right time and git show them to find the right one, then git update-index --add --cacheinfo 10644,objectidhere,path/to/it to restore the index entry manually.

edit: or if you haven't edited the content in your worktree since git checkout or any later git add, then just git add it again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.