Removing multiple files from a Git repo that have already been deleted from disk
Solution 1
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 .
Solution 2
git ls-files --deleted -z | xargs -0 git rm
might be what you are looking for.. it works for me..
Solution 3
You can use
git add -u
To add the deleted files to the staging area, then commit them
git commit -m "Deleted files manually"
Solution 4
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
Solution 5
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).
Codebeef
Freelance Rails and iOS developer based in Manchester, UK. Developer behind Bouldr and it's iphone app. More projects available on my site, along with my portfolio and a little more about me. If you have a project you'd like helping out with, I'm available for freelance work.
Updated on December 22, 2021Comments
-
Codebeef over 2 years
I have a Git repo that I have deleted four files from using
rm
(notgit 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.-
kch over 14 years
-
Brett Ryan about 11 years@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. -
Isabelle Wedin over 10 yearsUgh, asking why someone doesn't use
git rm
is a bit like asking why they don't usegit vim
orgit 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. -
Roland about 10 yearsVarinder'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/…
-
Admin about 6 yearsPossible duplicate of How to add and commit removals made with "rm" instead of "git rm"?
-
jbo5112 over 4 years@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.
-
-
SpoonMeiser almost 15 yearsNot 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.
-
carl over 14 yearsWhile it'll work, I often find myself deleting a ton of files through just
rm
and regretting it later. -
Igor Zevaka over 14 yearsRight,
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. -
Emil Sit over 14 yearscommit -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.
-
Ian Hunter over 12 yearsNote that this will add all changed, tracked files--deleted and updated.
-
Jay Taylor over 12 yearsThe commandset beneath the "Update: .." worked like a charm. Thanks!
-
Keegan Crain about 12 yearsPlease note Google user: It adds the modified tracked files to the staging area as well.
-
mit about 12 yearsThank you so much for 'git ls-files --deleted | xargs git rm' !
-
Paul Prewett about 12 years@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]
-
spaaarky21 almost 12 yearsOn 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.
-
Ian Dunn over 11 yearsWhy 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. -
Eric Wilson over 11 years@Saeb Understand about the queue, but
xargs
is about 15 minutes to master. -
c.. over 11 yearsalso
git add -u folder/
to run this operation in a folder -
Aryo over 11 yearsRun this command only if you had just removed the files and want to stage them immediately.
-
reto over 11 years"git ls-files --deleted | xargs git rm" is the correct answer! Thanks!
-
Dan about 11 yearsfor 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'
-
flq about 11 yearsThat is actually the correct answer since -u also adds modified tracked files to the index.
-
Mark Reed about 11 years
git status | awk '/deleted/ {print $3}' | xargs git rm
would be a shorter way to do that.grep | awk
... Just Say No. -
Sheharyar about 11 yearsYou got down-voted because "-a" is not an acceptable switch, it's "-A". Editing your answer.
-
SpoonMeiser about 11 yearsNo, "-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.
-
Ramsharan about 11 yearsactually 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."
-
Hotschke about 11 years
git rm $(git ls-files --deleted)
isn't this more convenient ( copied from this). -
Casey about 11 yearsand from cmd line
git config --global alias.rmd '!git ls-files --deleted -z | xargs -0 git rm'
-
Adam about 11 years"git ls-files" is incorrect: this outputs filenames with literal spaces, that breaks the next part.
-
Adam about 11 years@Ramsharan no, because it doesnt do that at all. This deletes a single file; the OP SPECIFICALLY requested "all" deleted files.
-
Emil Sit about 11 years@Adam I've updated the syntax with flags that will parse correctly in
xargs
. -
Ramsharan about 11 years@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.
-
Adam about 11 years@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.
-
Ramsharan about 11 years@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.
-
maazza almost 11 yearsbe wary of files with spaces in their name
-
galarant almost 11 yearsgit commit -a -u -m 'commit message' will stage all changes including deletions, and commit all in one line
-
Travesty3 almost 11 yearsI 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. -
Dave Everitt almost 11 years@maazza - how do you handle files with spaces in their name?
-
B T over 10 yearsHuh, 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 *
? -
Andrew over 10 yearsxargs 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
-
Max Nanasy over 10 years@DaveEveritt
git ls-files --deleted -z | xargs -0 git rm
-
Jakub M. over 10 yearsbit more elegant:
git status | awk '/deleted/{print "git rm " $3}' | sh
-
Dennis over 10 yearsWell 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'.
-
fazy over 10 yearsPreview 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. -
Ajedi32 over 10 years@Andrew
xargs
is probably more efficient than a while loop for a huge list, since it passes more than one argument each time it executesgit rm
. To limit the number of arguments passed togit rm
each time it is executed, use the-n
option:git ls-files --deleted | xargs -n 15 git rm
-
Andrew over 10 years@Ajedi32 learned something new:
xargs -n
is very winning. Thanks! -
Popnoodles over 10 years@maazza please don't radically change answers. This one is three-year-old. The change should not have been approved.
-
Sawyer over 10 years'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' for the situation when the file path has space
-
AJP about 10 yearsPlease see @Hotschke's
git rm $(git ls-files --deleted)
comment on Saeb's answer. -
Tara Roys about 10 yearsThis 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 :/
-
Roland about 10 yearsgit add -u as Cody suggested is a much simpler and safer way, no risk of accidentally deleting files.
-
donkey about 10 yearsI 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/ -
MIWMIB almost 10 yearsI also like using
git clean -f -d
before issuinggit add -u
when I don't have any new files to start tracking. -
Shog9 almost 10 yearsFYI: this answer merged from stackoverflow.com/questions/1402776/…
-
Shog9 almost 10 yearsFYI: this answer merged from stackoverflow.com/questions/1402776/…
-
Shog9 almost 10 yearsFYI: this answer merged from stackoverflow.com/questions/1402776/…
-
Shog9 almost 10 yearsFYI: this answer merged from stackoverflow.com/questions/1402776/…
-
Shog9 almost 10 yearsFYI: this answer merged from stackoverflow.com/questions/1402776/…
-
TheWarriorNamedFoo over 9 yearsThe 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. -
SteveShaffer about 9 yearsSeems to be working on PowerShell (not git shell or anything, just generic PowerShell with git.exe installed) and it worked!
-
Julien Palard about 9 yearsWon't work with files containing spaces in their name. Use
git add -u
instead shorter and safer. -
Shawn Whinnery about 9 yearsWould you please elaborate on what this does?
-
Zac Grierson about 9 yearsAt this stage you might as well add all via the commit -a flag.
-
Rafael Eyng about 9 years
git ls-files --deleted
yields all files with statusdeleted
. For each deleted file, git does agit rm
. -
n13 almost 9 yearsDownvoting 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.
-
kmarabet over 8 yearsgit add --all && git commit -m "Your commit" This worked for me in windows-7, using Git bash command shell.
-
kmarabet over 8 yearsThe 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.
-
Hossam Badri over 8 yearsI think you should always use git shell on you current working directory
-
DrB over 8 yearsThis is nice and simple but doesn't work with recursive folders.
-
Hossam Badri over 8 years@BehnazChangizi I think it does, stackoverflow.com/questions/1054044/…
-
Relequestual almost 8 yearsIt 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.
-
Hossam Badri almost 8 years@PvdL not accepted spaces in the path is os-specefic problem
-
maxkoryukov almost 8 yearssuch a simple action, and this difficult enough solution.. git-way
-
David Grayson over 7 yearsWindows 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.
-
iratzhash about 7 yearsthis does not help when there are spaces in filenames.
-
9ilsdx 9rvj 0lo almost 7 yearsDownvote because the solution is dangerous and instable. A pity it's accepted by the OP.
-
srinivas gowda over 6 yearsi am not able to remove file from bitbucket repo]
-
David Rogers almost 6 yearsFYI: 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.
-
billrichards almost 6 yearsIn case the only thing you're committing is the deleted file, add the switch --allow-empty
-
billrichards almost 6 yearserr, actually --allow-empty is only needed if you did git rm --cached
-
mlncn over 5 yearsWildcards 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). -
jbo5112 over 4 years
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<cmd1> | xargs <cmd2>
instead of<cmd2> $(<cmd1>)
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 over 4 yearsAdding 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".
-
bbennett36 over 4 yearsWARNING: THIS DELETES THE FILES ON DISK TOO. I just accidentally deleted tons of files...
-
user151841 over 4 years2019 git version 2.13.3:
git diff --diff-filter=D --name-only | xargs git rm
-
jlh over 3 yearsAwesome! You may want to add
-r
to xargs, so it won't fail if there happens to not be any deleted file. -
ElectricErger about 2 yearsIMO reset is the best one since you could have deleted the file. It's too bad that there's 1000 upvotes on add.