I have moved a bunch of files into another folder in a git repo and git status
shows something like
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
deleted: file1.ini
deleted: file2.ini
deleted: stm_1.py
deleted: stm_2.py
And I want to remove them together. I can remove each file with
git rm <filename>
but when you have many files that would take a long time. So I tried to use
git rm -r .
but that gives an error
error: the following files have changes staged in the index:
and I tried
got rm -r -f .
but that removed everything. Is there a command I can use to remove only those files that I see “deleted” in git status
?
1
-
Use git status to list the deleted files and then filter out the filenames.
-
Use xargs to pass those filenames to git rm
git status –porcelain | awk ‘/^ D/ {print $2}’ | xargs git rm
Explanation:
git status –porcelain provides a simplified and script-friendly output of git status.
awk ‘/^ D/ {print $2}’ filters lines that start with D (indicating a deleted file) and prints the filename (which is the second field).
xargs git rm takes the list of filenames and passes them to git rm to remove them from the index.
1