115

First off, I'm new to Git.

I deleted a bunch of files locally on my Mac using Finder. I want the files that I deleted to no longer show in the current branch, but they do.

Any Git users know a command to update the index?

Zack
  • 2,846
  • 2
  • 22
  • 16
  • See also http://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk – Petr Gladkikh Nov 15 '12 at 10:51

4 Answers4

225

I think this would be a simpler way to do what you want:

git add . -A 

Then you would just do:

git commit -m "removed some files"

As noted above.

Samuel Mikel Bowles
  • 2,369
  • 1
  • 13
  • 7
  • Thank you. And I had just written alias in `bash` to do the above. Amazing. – Zack Feb 20 '10 at 16:36
  • If you added a file in the same "stage" git will assume a rename. Anything to do about that or should you commit those separately? – Koen Mar 01 '12 at 10:22
  • 1
    @KimPrince - According to [kernel.org](http://www.kernel.org/pub/software/scm/git/docs/git-add.html), "-A --all Like -u, but match against files in the working tree in addition to the index. That means that it will find new files as well as staging modified content and removing files that are no longer in the working tree." – j08691 Jul 18 '12 at 20:33
  • 3
    @KimPrince `git help add` will tell you what `-A` means and lists all other switches as well. – Marcin Orlowski Jan 12 '13 at 14:50
  • 1
    This does a lot more than just removing deleted files. It adds all files that are changed in the directory so this is not really a satisfactory answer. – Alex Aug 24 '16 at 21:19
19

You can see deleted files, which are still 'tracked' with:

git ls-files --deleted

To delete files from a branch, you can do something like this:

git ls-files --deleted -z | xargs -0 git rm

From man git-rm:

Remove files from the index, or from the working tree and the index. git-rm will not remove a file from just your working directory. (There is no option to remove a file 13 only from the work tree and yet keep it in the index; use /bin/rm if you want to do that.)

Finally, to commit the "removal" do something like:

git commit -m "removed some files"
miku
  • 181,842
  • 47
  • 306
  • 310
17

I don't know if this has been added to git since the previous answers, but I just used

git add -u
git commit -m "Removed some files"

to achieve the same thing.

Gareth
  • 5,693
  • 3
  • 28
  • 37
  • This works for the listed use case, but this does not just add the deletion of the files. It adds all of the files that have been changed in any way, so this is not generally a good way to remove all deleted files unless that's the only changes you've made. – Alex Aug 24 '16 at 21:23
2
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch deletefile.name' --prune-empty --tag-name-filter cat -- --all
git commit -m "Removed deletefile.name"
git push origin master --force

Replace deletefile.name with the file to remove. For in-depth detailed explanation go through the nice article https://help.github.com/articles/remove-sensitive-data

Chawathe Vipul S
  • 1,636
  • 15
  • 28