Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git rm -r --cached

Tags:

git

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.

like image 934
Ned Avatar asked Aug 14 '14 15:08

Ned


People also ask

How do I reverse git rm cached?

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.

Does git rm -- cached delete the file?

By default, the git rm command deletes files both from the Git repository as well as the filesystem. Using the --cached flag, the actual file on disk will not be deleted.

What does git rm -- cached do?

The Git rm –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you'll still have a copy of the file locally. The file will be removed from the index tracking your Git project.

What is the difference between git rm -- cached and git reset?

git rm —cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD — file will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting.


5 Answers

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.

like image 164
Anshul Goyal Avatar answered Oct 12 '22 15:10

Anshul Goyal


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 . 
like image 33
big kev Avatar answered Oct 12 '22 15:10

big kev


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>
like image 43
Thiguet Cabral Avatar answered Oct 12 '22 16:10

Thiguet Cabral


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
like image 45
Hesham Yassin Avatar answered Oct 12 '22 14:10

Hesham Yassin


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

like image 29
Juancho Ramone Avatar answered Oct 12 '22 15:10

Juancho Ramone