Git 在提交后仍然显示已删除的文件

如何从 Git 回购中删除已删除的文件?

我删除了一个 JavaScript 库的文件夹,其中包含许多文件。然后我像这样做了修改:

git add .
git commit "message"
git status

但它显示所有这些文件为“删除...”。

我怎么才能让他们消失呢?

92818 次浏览

You need to record that they are indeed meant to be deleted. The same way you record file changes.

Just instead of git add, you will use git rm.

you need to tell git that it is removed

git rm folder

or if you do not want to keep them in repo you can add them to .gitignore

If it lists the files under the "to be committed" section, then just proceed with the commit; the files will remain deleted. (Git tracks deletions too, not just changes.)

If it lists the files under the "changed but not updated" section, then you have two options:

  1. Undelete them by restoring the version in the index: git checkout path/to/folder
  2. Mark them deleted in Git, then commit: git rm -r path/to/folder

This will add deletes as well.

git add -u .

Check what's staged to be committed with:

git status

git add -u .

If you type git status and the result says up to date, but in red it says

deleted: folder/example0.jpg
deleted: folder/example1.jpg
deleted: folder/example2.jpg

You need to enter this for it to be removed permanently git add -u . then all the red text will be marked in green.

**** Dont forget the space between the letter u and the period

supposing say you want to remove a file and would not want it to be committed:

use the command:

git reset HEAD filename

and then do a git status to verify if the file to be removed is not appearing

then do a git commit

i find myself have an unexpected 'deleted' folder after i 'rm xxx' to delete some local file.

i first create an temp branch and commit the unwant 'deleted' folder and then delete that temp branch.

I was also having red colored deleted files when I took pull from upstream/master. I tried different things but nothing worked.
Eventually, I had to revert all changes (committed, staged, unstaged) for my forked branch and had to re-sync my repo with the upstream master branch.

git reset --hard upstream/master
git pull upstream master