我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。


当前回答

這不再是最新 Git 的問題(v2.17.1 在寫作時)。

.gitignore 文件最终忽略了跟踪但删除的文件. 您可以通过运行下列脚本来为自己测试此文件. 最终 git 状态声明应该报告“没有任何承诺”。

# Create an empty repository
mkdir gitignore-test
cd gitignore-test
git init

# Create a file and commit it
echo "hello" > file
git add file
git commit -m initial

# Add the file to gitignore and commit
echo "file" > .gitignore
git add .gitignore
git commit -m gitignore

# Remove the file and commit
git rm file
git commit -m "removed file"

# Reintroduce the file and check status.
# .gitignore is now respected - status reports "nothing to commit".
echo "hello" > file
git status

其他回答

这就是我如何解决我的问题:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD git push

在此,我们基本上正在试图在以前的任务中重写该特定文件的历史。

有关更多信息,您可以在此处参阅过滤器分支的男性页面。

来源:从存储库中删除敏感数据 - 使用过滤器分支

来源:Git:如何删除错误的大文件

把它移动出来,承诺,然后把它移动回来。

这对我来说在过去已经工作了,但可能有更“迷人的”方式来实现这一点。

如果您创建了 gitignore 文件,使用命令如 echo node_modules >>.gitignore,它不会工作。

窗口终端存储文件在 UCS-2 LE BOM 和 git 似乎不接受它。

您可以通过在笔记本中打开并使用 UTF-8 编码重新存储。

此分類上一篇

它现在工作。

我认为他们需要纠正这一点,因为做echo“filetoignore” >>.gitignore实际上看起来非常有用

.gitignore 将防止未跟踪的文件添加(没有添加 -f)到 Git 跟踪的文件集,但是, Git 将继续跟踪已跟踪的任何文件。

要停止跟踪文件,我们必须从指数中删除文件:

git rm --cached <file>

要重新删除文件夹和文件中的所有文件:

git rm -r --cached <folder>

将文件从主审查中删除,将在下一个任务中进行。

警告:虽然这不会从您的本地机器中删除物理文件,但它会在下一个 git 拖动时将从其他开发人员的机器中删除文件。

使用 git rm --cached 命令不会回答原始问题:

你怎样要强迫吉特完全忘记(一个文件)?

事实上,这个解决方案会导致文件在运行 git pull 时在存储库的每个其他例子中被删除!

强迫 Git 忘记文件的正确方式是由 GitHub 在这里记录的。

我推荐阅读文档,但基本上:

git fetch --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch full/path/to/file' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

只需用文件的完整路径取代完整/路径/到/文件,请确保您已将文件添加到.gitignore 文件中。

您还需要(暂时)允许您的存储库不快推进,因为您正在改变您的 Git 历史。