61

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.

Community
  • 1
  • 1
Ned
  • 3,961
  • 8
  • 31
  • 49

5 Answers5

122

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.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • I can't, I'm getting message from above, on git checkout, git reset, whatever I try. – Ned Aug 14 '14 at 16:12
  • 1
    @Ned Can you update your question with the exact command you ran, and complete error message you get? I tried the scenario you are telling, and it works for me, so there might be something different in your commands. – Anshul Goyal Aug 14 '14 at 16:26
  • Actually no, I had to clone my project into another folder, and start from scratch. It must be that I mess something with those files,but now sure what. However, your answer is valid, and it should work for other cases, mine was probably different for some other reason, and I will accept your answer so that it can help other guys. Thanks! – Ned Aug 23 '14 at 21:58
  • Just in case somebody runs into this issue, the solution by @mu無 works since it worked for me. – Farhat Nawaz Nov 07 '19 at 12:27
10

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 . 
big kev
  • 307
  • 2
  • 8
  • this didn't re-track my untracked file – Mike W Feb 01 '18 at 17:17
  • mu 無's answer is the best answer because it completely removes the cached files from history. Please ignore this answer, you might also want to add a new question to your issue. – big kev Feb 03 '18 at 05:22
  • I had pushed some properties files to git, then later i wanted to add it to `.gitignore`. So, i added it and issued `git rm -r --cached .` . I ran `git status` it showed it deleted other files along with my `prop` files. But then i ssued `git add . ` it only staged deletion of `prop` files. – Arpan Banerjee May 07 '20 at 06:33
  • Plus one for the `git add`, I got the info `hint: Use -f if you really want to add them.` I did it and it worked. – Timo Jul 12 '22 at 20:59
7

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>
Thiguet Cabral
  • 131
  • 2
  • 7
2

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

Juan Girini
  • 1,150
  • 2
  • 16
  • 31
1

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
Hesham Yassin
  • 4,341
  • 2
  • 21
  • 23