Ned
Ned

Reputation: 4141

Undo git rm -r --cached

My .gitignore file wasn't working, so I followed this question.

And now I have bunch of files that I need to commit, in order to proceed. I made a mistake and committed those, because I couldn't find a way to get rid of those.

Is there any way that I can revert all these actions? I can't do reset cause I now I get error: error: The following untracked working tree files would be overwritten by checkout.

Stupid stupid mistake, and I can find solution for this.

Upvotes: 67

Views: 39889

Answers (5)

Thiguet Cabral
Thiguet Cabral

Reputation: 171

If you want git to make git "retrack" your files, that once where removed by .gitignore or by

git rm --cached

you can do it so by using:

git add -f <files>

After that if you want to remove files from the staged area you can use:

git restore --staged <files>

Upvotes: 10

Juan Girini
Juan Girini

Reputation: 1168

If you want to revert it for only a particular file you can do git restore --staged <file>

Upvotes: 2

Hesham Yassin
Hesham Yassin

Reputation: 4431

Running git checkout HEAD path/to/file will not work if the file is removed from the cache.

What worked for me is to move to a new branch by running the command:

git checkout -b newBranch

Upvotes: 1

big kev
big kev

Reputation: 337

Git works based on file caching so if you removed everything from the cache you can just reverse the whole process by executing .This will add back the files that were being tracked and tell which ones have been modified since the last commit .

> git add . 

Upvotes: 11

Anshul Goyal
Anshul Goyal

Reputation: 76867

If you've run only git rm -r --cached, try doing a git reset HEAD . from within your repo root.

If you did a git commit -m "msg" after doing a git rm -r --cached, i.e., you committed the changes, then do git reset HEAD~1 to undo your last commit.

Upvotes: 136

Related Questions