1397

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.

6
  • 3
    related: stackoverflow.com/questions/1856654/… Commented Dec 6, 2009 at 21:35
  • 12
    @seth, it's not always convenient to use git rm, the removal could have been from a separate tool, IDE or file manager. Visual Studio for one can be a pain when removing/renaming files. Commented Feb 25, 2013 at 3:06
  • 75
    Ugh, asking why someone doesn't use git rm is a bit like asking why they don't use git vim or git cd. It's a stupid thing to have to do, and git should have a built-in command or alias to remove deleted files from staging, and you shouldn't have to look it up on SO or read man pages on your lunch break. Commented Aug 13, 2013 at 2:45
  • 8
    Varinder's answer is not a good way. Consider git add -u as Cody suggested and which is also the answer to this question: stackoverflow.com/questions/1402776/… Commented Feb 27, 2014 at 17:35
  • Possible duplicate of How to add and commit removals made with "rm" instead of "git rm"? Commented Apr 17, 2018 at 2:54

32 Answers 32

2372

For Git 1.x

$ git add -u 

This tells git to automatically stage tracked files -- including deleting the previously tracked files.

For Git 2.0

To stage your whole working tree:

$ git add -u :/ 

To stage just the current path:

$ git add -u . 
Sign up to request clarification or add additional context in comments.

11 Comments

Note that this will add all changed, tracked files--deleted and updated.
@beanland, you need only provide the path to the specific file you want to modify if you don't want it to get them all. e.g. git add -u [path]
also git add -u folder/ to run this operation in a folder
FYI: this answer merged from stackoverflow.com/questions/1402776/…
It might be the question people were asking when they arrived at this page, and it might be what the OP actually meant to ask, but it doesn't answer the exact question asked.
|
1402
git ls-files --deleted -z | xargs -0 git rm 

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

19 Comments

for windows try to add the following alias in your config without the quotes as stated above by Evan Moran 'remove = !git ls-files --deleted -z | xargs -0 git rm'
be wary of files with spaces in their name
@DaveEveritt git ls-files --deleted -z | xargs -0 git rm
git add -u as Cody suggested is a much simpler and safer way, no risk of accidentally deleting files.
Downvoting this as it is the wrong answer even if it might work for some files. use "git add -u" below. That's what it's for.
|
756

You can use

git add -u 

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

git commit -m "Deleted files manually" 

3 Comments

Please note Google user: It adds the modified tracked files to the staging area as well.
Run this command only if you had just removed the files and want to stage them immediately.
i am not able to remove file from bitbucket repo]
362

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 

9 Comments

Right, git commit -a will do what I want, except in some cases I have files that I don't want to commit, so i want to prepare the commit manually.
commit -a essentially does an "add -u" first; it will update the index with any changes to known files (be they deletions or simple changes). You can of course be more specific and add/rm only the files you want. git.or.cz/gitwiki/… may be helpful.
The commandset beneath the "Update: .." worked like a charm. Thanks!
Thank you so much for 'git ls-files --deleted | xargs git rm' !
"git ls-files --deleted | xargs git rm" is the correct answer! Thanks!
|
171

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).

2 Comments

On a quick side note, using -u limits the add to tracked files and -a will include untracked files as well. Just thought I would point out the difference.
At this stage you might as well add all via the commit -a flag.
98

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.

9 Comments

@Saeb Understand about the queue, but xargs is about 15 minutes to master.
git status | awk '/deleted/ {print $3}' | xargs git rm would be a shorter way to do that. grep | awk... Just Say No.
git rm $(git ls-files --deleted) isn't this more convenient ( copied from this).
xargs or the above $() substitution if you know the list isn't huge. If the list is huge: git ls-files --deleted | while read f; do git rm $f; done
@Andrew xargs is probably more efficient than a while loop for a huge list, since it passes more than one argument each time it executes git rm. To limit the number of arguments passed to git rm each time it is executed, use the -n option: git ls-files --deleted | xargs -n 15 git rm
|
65
git rm test.txt 

Before or after you deleted the actual file.

11 Comments

While it'll work, I often find myself deleting a ton of files through just rm and regretting it later.
Why is this worse than doing git add -u? It seems like it'd be safer to add the specific files that were deleted to the commit, rather than adding ALL changes.
actually this should be the best answer according to the question's last line "I just want a command that deletes all files from git that are also deleted from the disk."
@Ramsharan no, because it doesnt do that at all. This deletes a single file; the OP SPECIFICALLY requested "all" deleted files.
@Ramsharan no, that's the point - in almost all cases you CANNOT simply use a wildcard (there is no wildcard that will match). This is why there's the main answer is so much more complicated.
|
50

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.

Comments

41

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.

Comments

32

If those are the only changes, you can simply do

git commit -a 

to commit all changes. That will include deleted files.

5 Comments

Not sure why I got down-voted; git does a good job of identifying files that have been deleted, even if you've not explicitly told it by using git rm.
You got down-voted because "-a" is not an acceptable switch, it's "-A". Editing your answer.
No, "-a" is an acceptable switch, because the command is commit. "-A" is a switch to add but not commit. I see your suggested edit has already been rejected.
In case the only thing you're committing is the deleted file, add the switch --allow-empty
err, actually --allow-empty is only needed if you did git rm --cached
21
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. 

Comments

19

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.

1 Comment

The top-voted answer already says to use git add -u. Where does all of the other stuff in your answer come from, the documentation? You should link to the documentation and blockquote it if it does.
15

That simple solution works fine for me:

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

4 Comments

I think you should always use git shell on you current working directory
This is nice and simple but doesn't work with recursive folders.
@BehnazChangizi I think it does, stackoverflow.com/questions/1054044/…
@PvdL not accepted spaces in the path is os-specefic problem
15
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 easy to remember

Comments

12

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 

1 Comment

and from cmd line git config --global alias.rmd '!git ls-files --deleted -z | xargs -0 git rm'
10
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.

Comments

9

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.

1 Comment

IMO reset is the best one since you could have deleted the file. It's too bad that there's 1000 upvotes on add.
7

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" 

2 Comments

git add --all && git commit -m "Your commit" This worked for me in windows-7, using Git bash command shell.
The following command worked for me in windows-7, using Git bash command shell: $ git add --all && git commit -m "remove property files" [master 58c41ac] remove property files 1 file changed, 9 deletions(-) ... Then to push those committed changes to the remote repository have run: $ git push origin .. Counting objects: 10, done.
6

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

Comments

6

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 

Comments

5

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...

Comments

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

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

Comments

3

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.

2 Comments

Well using a gui is CHEATING, lol! But faster in the cases where the GUI designer covered your needs. It is good to know how to tinker with 'what is under the hood'.
FYI: this answer merged from stackoverflow.com/questions/1402776/…
3

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.

Comments

2

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.

Comments

2

(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 

Comments

1

something like

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

may do it.

1 Comment

2019 git version 2.13.3: git diff --diff-filter=D --name-only | xargs git rm
1

For visual studio project

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

which is useful when the deleted file path has space

Comments

1

Deleting all files ending with .log from Repo but not local storage

git rm --cached $(git ls-files | grep "\.log$")

Comments

0

Just simply

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.