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

526,064

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

Share:
526,064
Codebeef
Author by

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, 2021

Comments

  • Codebeef
    Codebeef over 2 years

    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.

    • kch
      kch over 14 years
    • Brett Ryan
      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
      Isabelle Wedin over 10 years
      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.
    • Roland
      Roland about 10 years
      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/…
    • Admin
      Admin about 6 years
    • jbo5112
      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
    SpoonMeiser almost 15 years
    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.
  • carl
    carl over 14 years
    While it'll work, I often find myself deleting a ton of files through just rm and regretting it later.
  • Igor Zevaka
    Igor Zevaka over 14 years
    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.
  • Emil Sit
    Emil Sit over 14 years
    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.
  • Ian Hunter
    Ian Hunter over 12 years
    Note that this will add all changed, tracked files--deleted and updated.
  • Jay Taylor
    Jay Taylor over 12 years
    The commandset beneath the "Update: .." worked like a charm. Thanks!
  • Keegan Crain
    Keegan Crain about 12 years
    Please note Google user: It adds the modified tracked files to the staging area as well.
  • mit
    mit about 12 years
    Thank you so much for 'git ls-files --deleted | xargs git rm' !
  • Paul Prewett
    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
    spaaarky21 almost 12 years
    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.
  • Ian Dunn
    Ian Dunn over 11 years
    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.
  • Eric Wilson
    Eric Wilson over 11 years
    @Saeb Understand about the queue, but xargs is about 15 minutes to master.
  • c..
    c.. over 11 years
    also git add -u folder/ to run this operation in a folder
  • Aryo
    Aryo over 11 years
    Run this command only if you had just removed the files and want to stage them immediately.
  • reto
    reto over 11 years
    "git ls-files --deleted | xargs git rm" is the correct answer! Thanks!
  • Dan
    Dan about 11 years
    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'
  • flq
    flq about 11 years
    That is actually the correct answer since -u also adds modified tracked files to the index.
  • Mark Reed
    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
    Sheharyar about 11 years
    You got down-voted because "-a" is not an acceptable switch, it's "-A". Editing your answer.
  • SpoonMeiser
    SpoonMeiser about 11 years
    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.
  • Ramsharan
    Ramsharan about 11 years
    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."
  • Hotschke
    Hotschke about 11 years
    git rm $(git ls-files --deleted) isn't this more convenient ( copied from this).
  • Casey
    Casey about 11 years
    and from cmd line git config --global alias.rmd '!git ls-files --deleted -z | xargs -0 git rm'
  • Adam
    Adam about 11 years
    "git ls-files" is incorrect: this outputs filenames with literal spaces, that breaks the next part.
  • Adam
    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
    Emil Sit about 11 years
    @Adam I've updated the syntax with flags that will parse correctly in xargs.
  • Ramsharan
    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
    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
    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
    maazza almost 11 years
    be wary of files with spaces in their name
  • galarant
    galarant almost 11 years
    git commit -a -u -m 'commit message' will stage all changes including deletions, and commit all in one line
  • Travesty3
    Travesty3 almost 11 years
    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.
  • Dave Everitt
    Dave Everitt almost 11 years
    @maazza - how do you handle files with spaces in their name?
  • B T
    B T over 10 years
    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 *?
  • Andrew
    Andrew over 10 years
    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
  • Max Nanasy
    Max Nanasy over 10 years
    @DaveEveritt git ls-files --deleted -z | xargs -0 git rm
  • Jakub M.
    Jakub M. over 10 years
    bit more elegant:git status | awk '/deleted/{print "git rm " $3}' | sh
  • Dennis
    Dennis over 10 years
    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'.
  • fazy
    fazy over 10 years
    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.
  • Ajedi32
    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 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
  • Andrew
    Andrew over 10 years
    @Ajedi32 learned something new: xargs -n is very winning. Thanks!
  • Popnoodles
    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
    Sawyer over 10 years
    'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' for the situation when the file path has space
  • AJP
    AJP about 10 years
    Please see @Hotschke's git rm $(git ls-files --deleted) comment on Saeb's answer.
  • Tara Roys
    Tara Roys about 10 years
    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 :/
  • Roland
    Roland about 10 years
    git add -u as Cody suggested is a much simpler and safer way, no risk of accidentally deleting files.
  • donkey
    donkey about 10 years
    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/
  • MIWMIB
    MIWMIB almost 10 years
    I also like using git clean -f -d before issuing git add -u when I don't have any new files to start tracking.
  • Shog9
    Shog9 almost 10 years
    FYI: this answer merged from stackoverflow.com/questions/1402776/…
  • Shog9
    Shog9 almost 10 years
    FYI: this answer merged from stackoverflow.com/questions/1402776/…
  • Shog9
    Shog9 almost 10 years
    FYI: this answer merged from stackoverflow.com/questions/1402776/…
  • Shog9
    Shog9 almost 10 years
    FYI: this answer merged from stackoverflow.com/questions/1402776/…
  • Shog9
    Shog9 almost 10 years
    FYI: this answer merged from stackoverflow.com/questions/1402776/…
  • TheWarriorNamedFoo
    TheWarriorNamedFoo over 9 years
    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.
  • SteveShaffer
    SteveShaffer about 9 years
    Seems to be working on PowerShell (not git shell or anything, just generic PowerShell with git.exe installed) and it worked!
  • Julien Palard
    Julien Palard about 9 years
    Won't work with files containing spaces in their name. Use git add -u instead shorter and safer.
  • Shawn Whinnery
    Shawn Whinnery about 9 years
    Would you please elaborate on what this does?
  • Zac Grierson
    Zac Grierson about 9 years
    At this stage you might as well add all via the commit -a flag.
  • Rafael Eyng
    Rafael Eyng about 9 years
    git ls-files --deleted yields all files with status deleted. For each deleted file, git does a git rm.
  • n13
    n13 almost 9 years
    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.
  • kmarabet
    kmarabet over 8 years
    git add --all && git commit -m "Your commit" This worked for me in windows-7, using Git bash command shell.
  • kmarabet
    kmarabet over 8 years
    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.
  • Hossam Badri
    Hossam Badri over 8 years
    I think you should always use git shell on you current working directory
  • DrB
    DrB over 8 years
    This is nice and simple but doesn't work with recursive folders.
  • Hossam Badri
    Hossam Badri over 8 years
    @BehnazChangizi I think it does, stackoverflow.com/questions/1054044/…
  • Relequestual
    Relequestual almost 8 years
    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.
  • Hossam Badri
    Hossam Badri almost 8 years
    @PvdL not accepted spaces in the path is os-specefic problem
  • maxkoryukov
    maxkoryukov almost 8 years
    such a simple action, and this difficult enough solution.. git-way
  • David Grayson
    David Grayson over 7 years
    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.
  • iratzhash
    iratzhash about 7 years
    this does not help when there are spaces in filenames.
  • 9ilsdx 9rvj 0lo
    9ilsdx 9rvj 0lo almost 7 years
    Downvote because the solution is dangerous and instable. A pity it's accepted by the OP.
  • srinivas gowda
    srinivas gowda over 6 years
    i am not able to remove file from bitbucket repo]
  • David Rogers
    David Rogers almost 6 years
    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.
  • billrichards
    billrichards almost 6 years
    In case the only thing you're committing is the deleted file, add the switch --allow-empty
  • billrichards
    billrichards almost 6 years
    err, actually --allow-empty is only needed if you did git rm --cached
  • mlncn
    mlncn over 5 years
    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).
  • jbo5112
    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
    jbo5112 over 4 years
    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".
  • bbennett36
    bbennett36 over 4 years
    WARNING: THIS DELETES THE FILES ON DISK TOO. I just accidentally deleted tons of files...
  • user151841
    user151841 over 4 years
    2019 git version 2.13.3: git diff --diff-filter=D --name-only | xargs git rm
  • jlh
    jlh over 3 years
    Awesome! You may want to add -r to xargs, so it won't fail if there happens to not be any deleted file.
  • ElectricErger
    ElectricErger about 2 years
    IMO reset is the best one since you could have deleted the file. It's too bad that there's 1000 upvotes on add.