Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple deleted files in Git repository

Tags:

git

git-rm

People also ask

How do I remove a deleted file from my git repository?

Delete Files using git rm. The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

Does git add/remove deleted files?

To add a single file to the commit that you've deleted, you can do git add what/the/path/to/the/file/used/to/be . This is helpful when you have one or two deletions to add, but doesn't add a batch of deletions in one command.

How do I delete all deleted files?

Wipe files The most common way of wiping deleted files from your hard drive is to permanently remove them with data wiping software. Pros: Wiping files with data wiping software is a simple and straightforward option that doesn't require you to take any additional steps. Just select the file and wipe it.


git add -u 

updates all your changes


Be very cautious about git rm .; it might remove more than you want. Of course, you can recover, but it is simpler not to have to do so.

Simplest would be:

git rm modules/welcome/language/english/kaimonokago_lang.php \
       modules/welcome/language/french/kaimonokago_lang.php \
       modules/welcome/language/german/kaimonokago_lang.php \
       modules/welcome/language/norwegian/kaimonokago_lang.php

You can't use shell wildcards because the files don't exist, but you could use (in Bash at least):

git rm modules/welcome/language/{english,french,german,norwegian}/kaimonokago_lang.php

Or consider:

git status | sed -n '/^# *deleted:/s///p' | xargs git rm

This takes the output of git status, doesn't print anything by default (sed -n), but on lines that start # deleted:, it gets rid of the # and the deleted: and prints what is left; xargs gathers up the arguments and provides them to a git rm command. This works for any number of files regardless of similarity (or dissimilarity) in the names.


Another version to ByScripts answer is

git rm $(git ls-files --deleted)

This will ONLY remove the deleted files from the git.

It could be also be used for adding ONLY modified files also.

git add $(git ls-files --modified)

These commands also works on gitbash for windows.


Update all changes you made:

git add -u

The deleted files should change from unstaged (usually red color) to staged (green). Then commit to remove the deleted files:

git commit -m "note"

The best solution if you don't care about staging modified files is to use git add -u as said by mshameers and/or pb2q.

If you just want to remove deleted files, but not stage any modified ones, I think you should use the ls-files argument with the --deleted option (no need to use regex or other complex args/options) :

git ls-files --deleted | xargs git rm

Yes, git rm <filename> will stage the deleted state of a file, where <filename> could be a glob pattern:

$ git rm modules/welcome/language/*/kaimonokago_lang.php
rm modules/welcome/language/english/kaimonokago_lang.php
rm modules/welcome/language/french/kaimonokago_lang.php
rm modules/welcome/language/german/kaimonokago_lang.php
rm modules/welcome/language/norwegian/kaimonokago_lang.php

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    modules/welcome/language/english/kaimonokago_lang.php
#       ...

Then, you can commit.

git commit -a will do this in one go, if you want.

You can also use git add -u to stage all the changes, including all the deleted files, then commit.


When I have a lot of files I've deleted that are unstaged for commit, you can git rm them all in one show with:

for i in `git status | grep deleted | awk '{print $3}'`; do git rm $i; done

As question answerer mentioned, be careful with git rm.