1384

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.

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Codebeef
  • 43,508
  • 23
  • 86
  • 119
  • 3
    related: http://stackoverflow.com/questions/1856654/git-how-to-add-commit-removals-made-via-vanilla-rm – kch Dec 06 '09 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. – Brett Ryan Feb 25 '13 at 03:06
  • 73
    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. – Isabelle Wedin Aug 13 '13 at 02: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: http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Roland Feb 27 '14 at 17:35
  • Possible duplicate of [How to add and commit removals made with "rm" instead of "git rm"?](https://stackoverflow.com/questions/1856654/how-to-add-and-commit-removals-made-with-rm-instead-of-git-rm) –  Apr 17 '18 at 02:54
  • @Roland `git add -u` will add all modified files that are being tracked. It make work because of circumstances (everything else has been committed), but if you're hoping it will just synchronize your deletes, then you may have some work finding the proper commit for some edits later. – jbo5112 Sep 17 '19 at 23:07

32 Answers32

2366

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 .
carl
  • 49,756
  • 17
  • 74
  • 82
  • 142
    Note that this will add *all* changed, tracked files--deleted and updated. – Ian Hunter Aug 11 '11 at 17:55
  • 20
    @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] – Paul Prewett Apr 10 '12 at 21:35
  • 28
    also `git add -u folder/` to run this operation in a folder – c.. Dec 12 '12 at 18:03
  • Huh, this is not working for me on git 1.8.1.msysgit.1 on windows 7 32-bit. Maybe I need to way `git add -u *`? – B T Jul 30 '13 at 21:39
  • Please see @Hotschke's `git rm $(git ls-files --deleted)` comment on Saeb's answer. – AJP Jan 27 '14 at 11:10
  • 1
    This command no longer works for git 2.0. According to the error message I got when I tried it, the proper command is `git add --update :/` – Tara Roys Feb 12 '14 at 02:38
  • I also like using `git clean -f -d` before issuing `git add -u` when I don't have any new files to start tracking. – MIWMIB Jul 16 '14 at 00:06
  • 3
    FYI: this answer merged from http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Shog9 Jul 24 '14 at 15:51
  • 4
    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. – Relequestual Jun 02 '16 at 08:48
  • FYI: some of the deleted files may show up as renames using this technique if, for example, the same file now exists (added) in a different (added) directory. – David Rogers May 21 '18 at 17:09
  • Adding all tracked files that have been updated is probably not what you want. I hope you don't actually need to revert to something you accidentally committed under this. It might be hard to track down under a comment of something like "removing deleted files from git". – jbo5112 Sep 17 '19 at 23:11
1390
git ls-files --deleted -z | xargs -0 git rm 

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

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Varinder
  • 14,005
  • 1
  • 15
  • 4
  • 6
    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' – C.LS Jan 31 '13 at 14:11
  • That is actually the correct answer since -u also adds modified tracked files to the index. – flq Feb 04 '13 at 20:48
  • 22
    be wary of files with spaces in their name – maazza May 14 '13 at 13:23
  • 1
    @maazza - how do you handle files with spaces in their name? – Dave Everitt Jul 13 '13 at 15:15
  • 46
    @DaveEveritt `git ls-files --deleted -z | xargs -0 git rm` – Max Nanasy Aug 23 '13 at 23:31
  • 2
    @maazza please don't radically change answers. This one is three-year-old. The change should not have been approved. – Popnoodles Jan 07 '14 at 11:14
  • 'git ls-files --deleted | sed 's/\(.*\)/"\1"/'| xargs git rm' for the situation when the file path has space – Sawyer Jan 15 '14 at 17:16
  • 16
    git add -u as Cody suggested is a much simpler and safer way, no risk of accidentally deleting files. – Roland Feb 27 '14 at 17:40
  • 1
    I think I have a problem using the solution mentioned above because i have so many deleted files(discarded sub-project), it literally tells me `-bash: /usr/bin/git: Argument list too long`. Answer should be @MaxNanasy 's/ – donkey Mar 15 '14 at 23:29
  • Seems to be working on PowerShell (not git shell or anything, just generic PowerShell with git.exe installed) and it worked! – SteveShaffer Mar 09 '15 at 15:29
  • 1
    Won't work with files containing spaces in their name. Use `git add -u` instead shorter and safer. – Julien Palard Mar 16 '15 at 11:29
  • Would you please elaborate on what this does? – Shawn Whinnery Mar 31 '15 at 22:51
  • `git ls-files --deleted` yields all files with status `deleted`. For each deleted file, git does a `git rm`. – Rafael Eyng Apr 17 '15 at 13:49
  • 6
    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. – n13 May 19 '15 at 08:44
  • 1
    such a simple action, and this difficult enough solution.. **git-way** – maxkoryukov Jul 13 '16 at 18:11
  • 1
    Windows users should get MSYS2 or "Git for Windows" so they can run a normal bash environment (like the rest of the world) and use this command. – David Grayson Aug 30 '16 at 17:16
  • Downvote because the solution is dangerous and instable. A pity it's accepted by the OP. – 9ilsdx 9rvj 0lo Jul 20 '17 at 10:01
  • 1
    `git add -u` will add files that have either been updated or deleted. It could cause problems for people trying to sort out their changes, so it shouldn't be the proper answer. Also, I'm glad the update stood. Using ` | xargs ` instead of ` $()` is not a radical change @Popnoodles, and it works better. Remember, in order for stackoverflow to be useful, people need to easily find good answers. Even at 8.5 years old, this answer was my first solid search result on Google, so don't act like the information went bad. – jbo5112 Sep 17 '19 at 22:54
  • Awesome! You may want to add `-r` to xargs, so it won't fail if there happens to not be any deleted file. – jlh Jan 22 '21 at 15:17
753

You can use

git add -u

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

git commit -m "Deleted files manually"
Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
361

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
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
  • 2
    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. – Igor Zevaka Sep 10 '09 at 00:22
  • 2
    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. http://git.or.cz/gitwiki/GitFaq#Whyis.22gitrm.22nottheinverseof.22gitadd.22.3F may be helpful. – Emil Sit Sep 10 '09 at 01:06
  • 2
    The commandset beneath the "Update: .." worked like a charm. Thanks! – Jay Taylor Sep 02 '11 at 16:46
  • 10
    Thank you so much for 'git ls-files --deleted | xargs git rm' ! – mit Mar 31 '12 at 20:52
  • 18
    "git ls-files --deleted | xargs git rm" is the correct answer! Thanks! – reto Jan 03 '13 at 10:15
  • "git ls-files" is incorrect: this outputs filenames with literal spaces, that breaks the next part. – Adam Apr 21 '13 at 20:15
  • @Adam I've updated the syntax with flags that will parse correctly in `xargs`. – Emil Sit Apr 22 '13 at 02:54
  • git commit -a -u -m 'commit message' will stage all changes including deletions, and commit all in one line – galarant Jun 14 '13 at 21:34
  • FYI: this answer merged from http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Shog9 Jul 24 '14 at 15:51
170

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

Dustin
  • 89,080
  • 21
  • 111
  • 133
97

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.

PuzzledVacuum
  • 453
  • 4
  • 9
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • 2
    @Saeb Understand about the queue, but `xargs` is about 15 minutes to master. – Eric Wilson Sep 19 '12 at 18:41
  • 11
    `git status | awk '/deleted/ {print $3}' | xargs git rm` would be a shorter way to do that. `grep | awk`... Just Say No. – Mark Reed Feb 19 '13 at 03:06
  • 62
    `git rm $(git ls-files --deleted)` isn't this more convenient ( copied from [this](http://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk)). – Hotschke Mar 26 '13 at 10:13
  • 1
    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 Aug 15 '13 at 19:31
  • 1
    bit more elegant:`git status | awk '/deleted/{print "git rm " $3}' | sh` – Jakub M. Aug 29 '13 at 18:47
  • 1
    Preview version of Saeb's answer for the nervous: `for x in ``git status | grep deleted | awk '{print $3}'``; do echo $x; done` (replace double backticks with single ones); SO's markup escaping doesn't seem to work. – fazy Nov 06 '13 at 11:24
  • 2
    @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` – Ajedi32 Nov 12 '13 at 14:29
  • @Ajedi32 learned something new: `xargs -n` is very winning. Thanks! – Andrew Dec 04 '13 at 18:57
  • 1
    FYI: this answer merged from http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Shog9 Jul 24 '14 at 15:51
65
git rm test.txt

Before or after you deleted the actual file.

hichris123
  • 10,145
  • 15
  • 56
  • 70
Peaker
  • 2,354
  • 1
  • 14
  • 19
  • 7
    While it'll work, I often find myself deleting a ton of files through just `rm` and regretting it later. – carl Sep 10 '09 at 00:15
  • 4
    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. – Ian Dunn Aug 07 '12 at 17:33
  • 2
    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 Mar 10 '13 at 03:36
  • 2
    @Ramsharan no, because it doesnt do that at all. This deletes a single file; the OP SPECIFICALLY requested "all" deleted files. – Adam Apr 21 '13 at 20:16
  • @Adam You are right. This single command cannot delete "all" deleted files. You have to run this command multiple times for multiple files. But you can use wild-card to delete multiple files. – Ramsharan Apr 24 '13 at 01:50
  • 1
    @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. – Adam Apr 24 '13 at 09:36
  • 1
    @Adam The main thing is I use "git rm filename" or "git rm folder/*" normally in my project. And normally it is enough for me. – Ramsharan Apr 24 '13 at 13:33
  • I can't believe this response got so many upvotes. All it did was give the basic command from the manual to remove one file at a time. The question asked for something "Similar to `git add .` which would add all new and modified files to the stage." This does not answer the question at all. – Travesty3 Jun 21 '13 at 13:54
  • FYI: this answer merged from http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Shog9 Jul 24 '14 at 15:51
  • this does not help when there are spaces in filenames. – Irtaza Feb 06 '17 at 08:56
  • WARNING: THIS DELETES THE FILES ON DISK TOO. I just accidentally deleted tons of files... – bbennett36 Sep 28 '19 at 17:24
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.

Malte Tancred
  • 268
  • 1
  • 5
zobie
  • 609
  • 4
  • 4
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.

Stephan Tual
  • 2,617
  • 3
  • 27
  • 49
31

If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • 3
    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. – SpoonMeiser Jul 26 '09 at 13:45
  • You got down-voted because "-a" is not an acceptable switch, it's "-A". Editing your answer. – Sheharyar Feb 21 '13 at 19:21
  • 11
    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. – SpoonMeiser Feb 23 '13 at 14:40
  • In case the only thing you're committing is the deleted file, add the switch --allow-empty – billrichards Jul 25 '18 at 18:11
  • err, actually --allow-empty is only needed if you did git rm --cached – billrichards Jul 25 '18 at 19:02
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.
Sijo Kurian
  • 476
  • 6
  • 15
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.

Obay
  • 191
  • 1
  • 2
  • [The top-voted answer](http://stackoverflow.com/a/1402793/456814) 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. – TheWarriorNamedFoo Aug 27 '14 at 03:19
14

That simple solution works fine for me:

git rm $(git ls-files --deleted)
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
12
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

alberand
  • 632
  • 5
  • 13
11

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
Evan Moran
  • 3,825
  • 34
  • 20
  • 1
    and from cmd line `git config --global alias.rmd '!git ls-files --deleted -z | xargs -0 git rm'` – Casey Apr 18 '13 at 12:53
9
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.

jonathan3692bf
  • 1,398
  • 1
  • 12
  • 14
8

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.

DarkNeuron
  • 7,981
  • 2
  • 43
  • 48
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"
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
ilgam
  • 4,092
  • 1
  • 35
  • 28
  • 1
    git add --all && git commit -m "Your commit" This worked for me in windows-7, using Git bash command shell. – kmarabet Nov 03 '15 at 15:36
  • 1
    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. – kmarabet Nov 03 '15 at 15:44
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

l3x
  • 30,760
  • 1
  • 55
  • 36
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
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Venkateswara Rao
  • 5,242
  • 1
  • 18
  • 13
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...

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
4
git commit -m 'commit msg' $(git ls-files --deleted)

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

Chad
  • 1,434
  • 1
  • 15
  • 30
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.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
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.

Serdar
  • 1,416
  • 2
  • 17
  • 43
  • 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'. – Dennis Sep 19 '13 at 18:42
  • 1
    FYI: this answer merged from http://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git – Shog9 Jul 24 '14 at 15:51
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.

Haris Krajina
  • 14,824
  • 12
  • 64
  • 81
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
urban
  • 5,392
  • 3
  • 19
  • 45
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

Sawyer
  • 568
  • 5
  • 17
1

something like

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

may do it.

Jeremy French
  • 11,707
  • 6
  • 46
  • 71
1

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

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

dahe
  • 806
  • 8
  • 13
0

Just simply

git add . && git commit -m "the message for commit" && git push
Kawaii Nea
  • 81
  • 1
  • 1
  • 11
0

You can commit all deleted files in Git by first using the command git add -u to stage the deleted files, and then using git commit -m "message" to commit the changes with a message describing the commit. The -u flag in the git add command stages all deleted files, as well as any other changes in the working directory.

Engr.Aftab Ufaq
  • 3,356
  • 3
  • 21
  • 47
-1

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

Broes De Cat
  • 536
  • 5
  • 14
  • Wildcards weren't working for me with `git add -u path/to/my/deleted-files-pattern-*.yml` so this was the best answer for me (not wanting to stage all deleted files, but 50 out of a couple hundred). – mlncn Nov 01 '18 at 00:57