79

How can I remove deleted files from my Git repo?

I've deleted a folder of a JavaScript library, which contained many files. I then went to commit the changes like so:

git add .
git commit "message"
git status

But it shows all those files as "deleted ....".

How can I make them go away?

random
  • 9,774
  • 10
  • 66
  • 83
Nik So
  • 16,683
  • 21
  • 74
  • 108
  • 1
    possible duplicate of [Remove all deleted files from "changed but not updated" in Git](http://stackoverflow.com/questions/3169787/remove-all-deleted-files-from-changed-but-not-updated-in-git) – Šimon Tóth Nov 29 '10 at 20:36
  • possible duplicate of [How do I commit all deleted files in Git?](http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git) – Ben Aug 29 '13 at 11:51

8 Answers8

149

This will add deletes as well.

git add -u .

Check what's staged to be committed with:

git status
Jamund Ferguson
  • 16,721
  • 3
  • 42
  • 50
41

If it lists the files under the "to be committed" section, then just proceed with the commit; the files will remain deleted. (Git tracks deletions too, not just changes.)

If it lists the files under the "changed but not updated" section, then you have two options:

  1. Undelete them by restoring the version in the index: git checkout path/to/folder
  2. Mark them deleted in Git, then commit: git rm -r path/to/folder
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 2
    I see, and if I have deleted multiple (in all likelihood up to dozens) small files scattered in different folders, do I have to do the git rm for each? Is there a shortcut? Thank You – Nik So Nov 29 '10 at 21:10
  • 6
    Assuming that you have no other changes to stage, or don't mind staging them, `git add -u` will stage all of the deletions at once. – cdhowie Nov 29 '10 at 21:11
22

git add -u .

If you type git status and the result says up to date, but in red it says

deleted: folder/example0.jpg
deleted: folder/example1.jpg
deleted: folder/example2.jpg

You need to enter this for it to be removed permanently git add -u . then all the red text will be marked in green.

**** Dont forget the space between the letter u and the period

mateostabio
  • 958
  • 8
  • 11
8

You need to record that they are indeed meant to be deleted. The same way you record file changes.

Just instead of git add, you will use git rm.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
2

you need to tell git that it is removed

git rm folder

or if you do not want to keep them in repo you can add them to .gitignore

mpapis
  • 52,729
  • 14
  • 121
  • 158
1

I was also having red colored deleted files when I took pull from upstream/master. I tried different things but nothing worked.
Eventually, I had to revert all changes (committed, staged, unstaged) for my forked branch and had to re-sync my repo with the upstream master branch.

git reset --hard upstream/master
git pull upstream master
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
0

supposing say you want to remove a file and would not want it to be committed:

use the command:

git reset HEAD filename

and then do a git status to verify if the file to be removed is not appearing

then do a git commit

0

i find myself have an unexpected 'deleted' folder after i 'rm xxx' to delete some local file.

i first create an temp branch and commit the unwant 'deleted' folder and then delete that temp branch.

RyanShao
  • 429
  • 2
  • 9