[git] Removing multiple files from a Git repo that have already been deleted from disk

I have a Git repo that I have deleted four files from using rm (not git rm), and my Git status looks like this:

#    deleted:    file1.txt
#    deleted:    file2.txt
#    deleted:    file3.txt
#    deleted:    file4.txt

How do I remove these files from Git without having to manually go through and add each file like this:

git rm file1 file2 file3 file4

Ideally, I'm looking for something that works in the same way that git add . does, if that's possible.

This question is related to git git-commit git-add git-rm

The answer is


something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.


git rm $(git ls-files -d)

Removes all files listed by the git ls-files command (-d show only deleted files). Doesn't work for files with spaces in the filename or path, but simple to remember


As mentioned

git add -u

stages the removed files for deletion, BUT ALSO modified files for update.

To unstage the modified files you can do

git reset HEAD <path>

if you like to keep your commits organized and clean.
NOTE: This could also unstage the deleted files, so careful with those wildcards.


I needed the same and used git gui "stage changed" button. it also adds all.

And after "stage changed" I made "commit" ...

so my working directory is clean again.


You can use git add -u <filenames> to stage the deleted files only.

For example, if you deleted the files templates/*.tpl, then use git add -u templates/*.tpl.

The -u is required in order to refer to files that exist in the repository but no longer exist in the working directory. Otherwise, the default of git add is to look for the files in the working directory, and if you specify files you've deleted there, it won't find them.


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

might be what you are looking for.. it works for me..


If you want to add it to your .gitconfig do this:

[alias]
  rma = !git ls-files --deleted -z | xargs -0 git rm

Then all you have to do is run:

git rma

(Yet another variation)

I wanted to delete all the already deleted from the disk files but from one specific folder, leaving the other folders untouched. The following worked for me:

git ls-files --deleted  | grep <folder-name> | xargs git rm

git add -u

-u --update Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.


If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.


git rm $(git ls-files -d)

Removes all files listed by the git ls-files command (-d show only deleted files). Doesn't work for files with spaces in the filename or path, but simple to remember


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

This will remove all deleted files that were previous tracked by git, as well as handle the case where your filenames have spaces in them.

Depending on your POSIX variant, you may need to use xargs -0 -r: this will cause xargs to gracefully exit when piped null content.

EDIT: --cached and --deleted flags are used in tandem to safeguard against accidentally deleting files that have not already been deleted.


For visual studio project

'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' 

which is useful when the deleted file path has space


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

This will remove all deleted files that were previous tracked by git, as well as handle the case where your filenames have spaces in them.

Depending on your POSIX variant, you may need to use xargs -0 -r: this will cause xargs to gracefully exit when piped null content.

EDIT: --cached and --deleted flags are used in tandem to safeguard against accidentally deleting files that have not already been deleted.


Please use -t to see which command is actually being ran

I just tweaked Virender answer to do same:

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

git ls-files --deleted | xargs git rm 

is the best option to add only deleted files.

Here is some other options.

git add .  => Add all (tracked and modified)/new files in the working tree.

git add -u => Add all modified/removed files which are tracked.

git add -A => Add all (tracked and modified)/(tracked and removed)/new files in the working tree.

git commit -a -m "commit message" - Add and commit modified/removed files which are tracked.

git add -u

-u --update Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.


You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"

Adding system alias for staging deleted files as command rm-all

UNIX alias rm-all='git rm $(git ls-files --deleted)'

WINDOWS doskey rm-all=bash -c "git rm $(git ls-files --deleted)"

Note

Windows needs to have bash installed.


Please use -t to see which command is actually being ran

I just tweaked Virender answer to do same:

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

None of the flags to git-add will only stage removed files; if all you have modified are deleted files, then you're fine, but otherwise, you need to run git-status and parse the output.

Working off of Jeremy's answer, this is what I got:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. Get status of files.
  2. For deleted files, isolate the name of the file.
  3. Remove all the lines that start with #s, as well as a status line that had the word "deleted" in it; I don't remember what it was, exactly, and it's not there any longer, so you may have to modify this for different situations. I think grouping of expressions might be a GNU-specific feature, so if you're not using gnutils, you may have to add multiple grep -v lines.
  4. Pass the files to git rm.

Sticking this in a shell alias now...


You're probably looking for -A:

git add -A

this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove command (although the move detection is automatic).


something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.


The following will work, even if you have a lot of files to process:

git ls-files --deleted | xargs git rm

You'll probably also want to commit with a comment.

For details, see: Useful Git Scripts


If you want to add it to your .gitconfig do this:

[alias]
  rma = !git ls-files --deleted -z | xargs -0 git rm

Then all you have to do is run:

git rma

Just simply

git add . && git commit -m "the message for commit" && git push

That simple solution works fine for me:

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

Adding system alias for staging deleted files as command rm-all

UNIX alias rm-all='git rm $(git ls-files --deleted)'

WINDOWS doskey rm-all=bash -c "git rm $(git ls-files --deleted)"

Note

Windows needs to have bash installed.


something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.


You're probably looking for -A:

git add -A

this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove command (although the move detection is automatic).


By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:

alias grma='git ls-files --deleted -z | xargs -0 git rm'

All files that have been removed from the file system are added to the index as deleted.


Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected:

-a
--all

git add . && git commit -m -a "Your commit"

or

git add --all && git commit -m "Your commit"

The following will work, even if you have a lot of files to process:

git ls-files --deleted | xargs git rm

You'll probably also want to commit with a comment.

For details, see: Useful Git Scripts


git ls-files --deleted | xargs git rm 

is the best option to add only deleted files.

Here is some other options.

git add .  => Add all (tracked and modified)/new files in the working tree.

git add -u => Add all modified/removed files which are tracked.

git add -A => Add all (tracked and modified)/(tracked and removed)/new files in the working tree.

git commit -a -m "commit message" - Add and commit modified/removed files which are tracked.

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

might be what you are looking for.. it works for me..


(Yet another variation)

I wanted to delete all the already deleted from the disk files but from one specific folder, leaving the other folders untouched. The following worked for me:

git ls-files --deleted  | grep <folder-name> | xargs git rm

git commit -m 'commit msg' $(git ls-files --deleted)

This worked for me after I had already deleted the files.


You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"

To stage only the deleted files:

for x in $(git status | grep deleted | awk '{print $2}'); do git rm $x; done

Or (the xargs way):

git status | awk '/deleted/ {print $2}' | xargs git rm

You can alias your preferred command set for convenient later use.


If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.


Not that it really matters, but I disagree with the chose answer:

git add -u 

... will remove files from the index if the corresponding files in the working tree have been removed, but it will also stage the modified new contents of tracked files.

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

... on the other hand will only rm the deleted files that were tracked.

So the latter in my view is the better option.


You're probably looking for -A:

git add -A

this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove command (although the move detection is automatic).


I needed the same and used git gui "stage changed" button. it also adds all.

And after "stage changed" I made "commit" ...

so my working directory is clean again.


If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.


By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:

alias grma='git ls-files --deleted -z | xargs -0 git rm'

All files that have been removed from the file system are added to the index as deleted.


None of the flags to git-add will only stage removed files; if all you have modified are deleted files, then you're fine, but otherwise, you need to run git-status and parse the output.

Working off of Jeremy's answer, this is what I got:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. Get status of files.
  2. For deleted files, isolate the name of the file.
  3. Remove all the lines that start with #s, as well as a status line that had the word "deleted" in it; I don't remember what it was, exactly, and it's not there any longer, so you may have to modify this for different situations. I think grouping of expressions might be a GNU-specific feature, so if you're not using gnutils, you may have to add multiple grep -v lines.
  4. Pass the files to git rm.

Sticking this in a shell alias now...


By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:

alias grma='git ls-files --deleted -z | xargs -0 git rm'

All files that have been removed from the file system are added to the index as deleted.


To stage only the deleted files:

for x in $(git status | grep deleted | awk '{print $2}'); do git rm $x; done

Or (the xargs way):

git status | awk '/deleted/ {print $2}' | xargs git rm

You can alias your preferred command set for convenient later use.


Not that it really matters, but I disagree with the chose answer:

git add -u 

... will remove files from the index if the corresponding files in the working tree have been removed, but it will also stage the modified new contents of tracked files.

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

... on the other hand will only rm the deleted files that were tracked.

So the latter in my view is the better option.


You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"

If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.


Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected:

-a
--all

git add . && git commit -m -a "Your commit"

or

git add --all && git commit -m "Your commit"

git commit -m 'commit msg' $(git ls-files --deleted)

This worked for me after I had already deleted the files.


something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.


The most flexible solution I have found to date is to

git cola

And select all deleted files I want to stage.

(Note I usually do everything commandline in git, but git handles removed files a bit awkward).


Just simply

git add . && git commit -m "the message for commit" && git push

As mentioned

git add -u

stages the removed files for deletion, BUT ALSO modified files for update.

To unstage the modified files you can do

git reset HEAD <path>

if you like to keep your commits organized and clean.
NOTE: This could also unstage the deleted files, so careful with those wildcards.


By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:

alias grma='git ls-files --deleted -z | xargs -0 git rm'

All files that have been removed from the file system are added to the index as deleted.


For visual studio project

'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' 

which is useful when the deleted file path has space


You're probably looking for -A:

git add -A

this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove command (although the move detection is automatic).


If you simply run:

git add -u

git will update its index to know that the files that you've deleted should actually be part of the next commit. Then you can run "git commit" to check in that change.

Or, if you run:

git commit -a

It will automatically take these changes (and any others) and commit them.

Update: If you only want to add deleted files, try:

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

You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"

git rm test.txt

Before or after you deleted the actual file.


The most flexible solution I have found to date is to

git cola

And select all deleted files I want to stage.

(Note I usually do everything commandline in git, but git handles removed files a bit awkward).


If you simply run:

git add -u

git will update its index to know that the files that you've deleted should actually be part of the next commit. Then you can run "git commit" to check in that change.

Or, if you run:

git commit -a

It will automatically take these changes (and any others) and commit them.

Update: If you only want to add deleted files, try:

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

git rm test.txt

Before or after you deleted the actual file.


You can use git add -u <filenames> to stage the deleted files only.

For example, if you deleted the files templates/*.tpl, then use git add -u templates/*.tpl.

The -u is required in order to refer to files that exist in the repository but no longer exist in the working directory. Otherwise, the default of git add is to look for the files in the working directory, and if you specify files you've deleted there, it won't find them.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-commit

How to undo the last commit in git git status (nothing to commit, working directory clean), however with changes commited Fix GitLab error: "you are not allowed to push code to protected branches on this project"? How to rollback everything to previous commit Troubleshooting misplaced .git directory (nothing to commit) Pushing empty commits to remote Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How do I commit case-sensitive only filename changes in Git? How to save a git commit message from windows cmd? Remove files from Git commit

Examples related to git-add

Fix GitLab error: "you are not allowed to push code to protected branches on this project"? Git add all subdirectories Staging Deleted files Adding Only Untracked Files Add all files to a commit except a single file? Git add all files modified, deleted, and untracked? Difference between "git add -A" and "git add ." Removing multiple files from a Git repo that have already been deleted from disk How can I add an empty directory to a Git repository?

Examples related to git-rm

Remove a folder from git tracking Staging Deleted files Why are there two ways to unstage a file in Git? How to remove multiple deleted files in Git repository "git rm --cached x" vs "git reset head --? x"? Git: list only "untracked" files (also, custom commands) Git add all files modified, deleted, and untracked? Git: How to remove file from index without deleting files from any repository How to revert a "git rm -r ."? How can I delete a file from a Git repository?