Undo git rm -r --cached

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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 . 

Solution 3 - Git

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>

Solution 4 - Git

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

Solution 5 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNedView Question on Stackoverflow
Solution 1 - GitAnshul GoyalView Answer on Stackoverflow
Solution 2 - Gitbig kevView Answer on Stackoverflow
Solution 3 - GitThiguet CabralView Answer on Stackoverflow
Solution 4 - GitHesham YassinView Answer on Stackoverflow
Solution 5 - GitJuancho RamoneView Answer on Stackoverflow