254

I have deleted some files and git status shows as below.

I have committed and pushed.

GitHub still shows the deleted files in the repository. How can I delete files in the GitHub repository?

# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    modules/welcome/language/english/kaimonokago_lang.php
#   deleted:    modules/welcome/language/french/kaimonokago_lang.php
#   deleted:    modules/welcome/language/german/kaimonokago_lang.php
#   deleted:    modules/welcome/language/norwegian/kaimonokago_lang.php

If I use git rm, it gives the following.

usage: git rm [options] [--] <file>...

-n, --dry-run         dry run
-q, --quiet           do not list removed files
--cached              only remove from the index
-f, --force           override the up-to-date check
-r                    allow recursive removal
--ignore-unmatch      exit with a zero status even if nothing matched
2

16 Answers 16

682
git add -u 

updates all your changes

4
  • 35
    Works a charm. When you see something like "git status | sed -n '/^# *deleted:/s///p' | xargs git rm" as a proposed solution for such a simple yet frequent requirement then you know a better solution is, or soon will be, around the corner.
    – arcseldon
    Commented Oct 4, 2013 at 10:10
  • simple is better, this is really handy then the "bash" way
    – castiel
    Commented Sep 5, 2014 at 10:46
  • 2
    Author asks how to remove files, why adding files is the correct answer?
    – kelin
    Commented Feb 12, 2016 at 13:40
  • 3
    @kelin: from git docs (git-scm.com/docs/git-add): "-u --update Update the index just where it already has an entry matching <pathspec>. This removes as well as modifies index entries to match the working tree, but adds no new files. If no <pathspec> is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories)."
    – HEDMON
    Commented Feb 23, 2016 at 6:40
92

Be very cautious about git rm .; it might remove more than you want. Of course, you can recover, but it is simpler not to have to do so.

Simplest would be:

git rm modules/welcome/language/english/kaimonokago_lang.php \
       modules/welcome/language/french/kaimonokago_lang.php \
       modules/welcome/language/german/kaimonokago_lang.php \
       modules/welcome/language/norwegian/kaimonokago_lang.php

You can't use shell wildcards because the files don't exist, but you could use (in Bash at least):

git rm modules/welcome/language/{english,french,german,norwegian}/kaimonokago_lang.php

Or consider:

git status | sed -n '/^# *deleted:/s///p' | xargs git rm

This takes the output of git status, doesn't print anything by default (sed -n), but on lines that start # deleted:, it gets rid of the # and the deleted: and prints what is left; xargs gathers up the arguments and provides them to a git rm command. This works for any number of files regardless of similarity (or dissimilarity) in the names.

6
  • 6
    You can use wildcards (though you may need to escape them), and git will match the paths in the index, not just those in the working tree, so it doesn't matter that they don't exist on the filesystem.
    – Ben James
    Commented May 14, 2011 at 22:20
  • 110
    git diff --diff-filter=D --name-only -z | xargs -0 git rm is a more reliable approach than trying to parse git status which is user oriented and not guaranteed to be stable over future versions.
    – CB Bailey
    Commented May 14, 2011 at 22:26
  • @Charles: that is certainly better, but it requires quite a lot of knowledge about what options are available with git diff (which I didn't have, not having had the need before). Thanks! Commented May 14, 2011 at 22:33
  • 8
    Note that there is also "git ls-files --deleted" Commented Nov 15, 2012 at 11:15
  • 2
    so, git ls-files --deleted | xargs git rm did the trick for me.
    – fiacobelli
    Commented Jan 23, 2015 at 16:34
55

Another version to ByScripts answer is

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

This will ONLY remove the deleted files from the git.

It could be also be used for adding ONLY modified files also.

git add $(git ls-files --modified)

These commands also works on gitbash for windows.

3
  • 2
    Priceless, the git ls-files with status deleted or modified is so helpful. Commented Sep 24, 2014 at 8:24
  • 1
    The behavior of 'git add --update (or -u)' with no path argument from a subdirectory of the tree will change in Git 2.0 and should not be used anymore. To add content for the whole tree, run: git add --update :/ (or git add -u :/) To restrict the command to the current directory, run: git add --update . (or git add -u .) With the current Git version, the command is restricted to the current directory
    – Ans
    Commented Dec 18, 2014 at 14:03
  • 1
    Any idea how to handle paths with spaces in them ? Example: src/my spaced directory/myfile. This can be handled individually but how can it be handled using the commands in this answer ? Commented Jun 3, 2015 at 17:33
41

Update all changes you made:

git add -u

The deleted files should change from unstaged (usually red color) to staged (green). Then commit to remove the deleted files:

git commit -m "note"
3
  • 1
    Deserves the populist badge.
    – John
    Commented May 1, 2015 at 16:19
  • 1
    This is by far the best solution. No scripting hacks, no lengthy process, just a flag to tell git to update its index thoroughly.
    – user1898811
    Commented Aug 10, 2015 at 15:22
  • Brilliant! Saved me from having to type 20+ file paths. Thanks! Commented Dec 11, 2017 at 18:36
13

The best solution if you don't care about staging modified files is to use git add -u as said by mshameers and/or pb2q.

If you just want to remove deleted files, but not stage any modified ones, I think you should use the ls-files argument with the --deleted option (no need to use regex or other complex args/options) :

git ls-files --deleted | xargs git rm
1
8

Yes, git rm <filename> will stage the deleted state of a file, where <filename> could be a glob pattern:

$ git rm modules/welcome/language/*/kaimonokago_lang.php
rm modules/welcome/language/english/kaimonokago_lang.php
rm modules/welcome/language/french/kaimonokago_lang.php
rm modules/welcome/language/german/kaimonokago_lang.php
rm modules/welcome/language/norwegian/kaimonokago_lang.php

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    modules/welcome/language/english/kaimonokago_lang.php
#       ...

Then, you can commit.

git commit -a will do this in one go, if you want.

You can also use git add -u to stage all the changes, including all the deleted files, then commit.

4
  • so rm . (full stop) delete them at once?
    – shin
    Commented May 14, 2011 at 20:11
  • so just git rm will remove all at once?
    – shin
    Commented May 14, 2011 at 20:13
  • 1
    You can use a glob-style pattern to remove multiple files; I updated my answer to show an example.
    – Ben James
    Commented May 14, 2011 at 20:21
  • 5
    It seems git add -u is exactly what we're looking for - if you make many deletions, then want to commit all the deletions, that appears to be the easiest and most 'standard' way (meaning, without case-specific globs or complex shell parsing into xargs). Is there a reason we wouldn't want to use this, other than the fact that it adds all changes at once?
    – xxx
    Commented Apr 26, 2012 at 17:16
7

When I have a lot of files I've deleted that are unstaged for commit, you can git rm them all in one show with:

for i in `git status | grep deleted | awk '{print $3}'`; do git rm $i; done

As question answerer mentioned, be careful with git rm.

5

Try this:

 git rm `git ls-files -d`
1
  • 1
    This solved my problem of staging only deleted scripts (using bash) Commented Oct 26, 2015 at 20:51
3

You can use

git commit -m "remove files" -a
git push

After you had delete files manually.

1
  • 1
    Or identically but more succinctly: git commit -am"remove files" Commented Feb 28, 2015 at 23:26
3

You can create a shell script which will remove all your files when run:

git status | grep deleted | awk '{print "git rm " $3;}' > ../remove.sh

The script that is created is remove.sh and it contains the full list of git rm commands.

3

I had this issue of ghost files appearing in my repo after I deleted them and came across this neat command!

git add -A

It's essentially the same as git add -a and git add -u combined, but it's case sensitive. I got it from this awesome link (this link points to the version on archive.org now, because the original has converted to a spam/phishing page as of June 2016)

3

If you want to delete all of them by using "git rm". This is what I do:

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

This query lists of all the files that have been removed and deletes them from your git repository. Hope it helps.

2
git status | sed 's/^#\s*deleted:\s*//' | sed 's/^#.*//' | xargs git rm -rf
2

The built in clean function can also be helpful...

git clean -fd
1
  • 2
    This only applies to untracked files. Commented May 15, 2013 at 19:34
2
git add -u .

git add --update .
0

Here is how to detect deleted files and stage their deletion as part of the next commit. All the solutions on this thread have different merits. This solution bellow specifically deals with the problem of file names with spaces in them.

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm '{}'

make sure you test this with git's --dry-run option before running it with the following:

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm --dry-run '{}'

explanation:

git status --porcelain

This prints out something like D "/path to a folder/path to a file" which happens only when there are spaces in the path names

awk '/^.D .*$/ {print $0}'

match only lines that start with " D "

sed 's/ D \(.*\)/\1/'

remove " D " from the front of each string

tr -d '"'

remove quotes, if any

xargs -I {} git rm '{}'

define file name variables as {} run file name under git rm enclosed in single quotes in order to make sure that it supports file names with spaces.

Not the answer you're looking for? Browse other questions tagged or ask your own question.