Staging Deleted files

GitGit AddGit RmGit Stage

Git Problem Overview


Say I have a file in my git repository called foo.

Suppose it has been deleted with rm (not git rm). Then git status will show:

Changes not staged for commit:

    deleted: foo

How do I stage this individual file deletion?

If I try:

git add foo

It says:

'foo' did not match any files.

Update (9 years later, lol):

This looks like it has been fixed in git 2.x:

$ git --version
git version 2.25.1

$ mkdir repo

$ cd repo

$ git init .
Initialized empty Git repository in repo/.git/

$ touch foo bar baz

$ git add foo bar baz

$ git commit -m "initial commit"
[master (root-commit) 79c736b] initial commit
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 bar
create mode 100644 baz
create mode 100644 foo

$ rm foo

$ git status
On branch master
Changes not staged for commit:
    deleted: foo

$ git add foo

$ git status
On branch master
Changes to be committed:
    deleted:    foo

Git Solutions


Solution 1 - Git

Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, if it hadn't been previously deleted. It can, of course, be restored from git, since it was previously checked in.)

To stage the file for deletion without deleting it from the file system, use git rm --cached foo

Solution 2 - Git

Even though it's correct to use git rm [FILE], alternatively, you could do git add -u.

According to the git-add documentation:

> -u > --update > > Update the index just where it already has an entry matching [FILE]. This removes as well as modifies index entries to match > the working tree, but adds no new files. > > If no [FILE] 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).

Upon which the index will be refreshed and files will be properly staged.

Solution 3 - Git

To stage all manually deleted files you can use:

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

To add an alias to this command as git rm-deleted, run:

git config --global alias.rm-deleted '!git rm $(git ls-files --deleted)'

Solution 4 - Git

Since Git 2.0.0, git add will also stage file deletions.

Git 2.0.0 Docs - git-add

> < pathspec >… > > Files to add content from. Fileglobs (e.g. *.c) can be given to add all > matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to update the index to match the current state of the directory as a whole (e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree. Note that older versions of Git used to ignore removed files; use --no-all option if you want to add modified or new files but ignore removed ones.

Solution 5 - Git

to Add all ready deleted files

git status -s | grep -E '^ D' | cut -d ' ' -f3 | xargs git add --all

thank check to make sure

git status

you should be good to go

Solution 6 - Git

You can use this command

git add `git ls-files --deleted`

Explanation:

git ls-files --deleted - This command will return the filenames of all deleted files.

Solution 7 - Git

You can use

git rm -r --cached -- "path/to/directory"

to stage a deleted directory.

Solution 8 - Git

Ian Mackinnon found the answer, but it's better with xargs:

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

As a git alias:

git config --global alias.rm-deleted '!git ls-files --deleted -z | xargs -r0 git rm'

This uses xargs with NUL termination (the only byte guarranteed not to appear in a path) and the option to not run git rm if the file list is empty.

This syntax is also fish compatible.

Solution 9 - Git

If you want to simply add all the deleted files to stage then you can use git add .

This is the easiest way right now with git v2.27.0. Note that using * and . are different approaches. Using git add * would only add currently present files whereas git add . would also stage the files deleted with rm command.

It's obvious but worth mentioning that other files which have been modified would also be added to the staging area when you use git add ..

Solution 10 - Git

for those using git 2.x+ in powershell:

foreach ($filePath in (git ls-files --deleted)) { git add "$filePath" }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew TomazosView Question on Stackoverflow
Solution 1 - GitWoobleView Answer on Stackoverflow
Solution 2 - GitSaileshView Answer on Stackoverflow
Solution 3 - GitIan MackinnonView Answer on Stackoverflow
Solution 4 - GitpeflorencioView Answer on Stackoverflow
Solution 5 - Gitchet coreyView Answer on Stackoverflow
Solution 6 - GitGnanaJeyamView Answer on Stackoverflow
Solution 7 - GitGameScriptingView Answer on Stackoverflow
Solution 8 - Gituser2394284View Answer on Stackoverflow
Solution 9 - GitAshutosh SinghView Answer on Stackoverflow
Solution 10 - Gitorion elenzilView Answer on Stackoverflow