我把以前由 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 忽略,那么你可以将其返回文件夹, Git 不会看到它。
把它移动出来,承诺,然后把它移动回来。
这对我来说在过去已经工作了,但可能有更“迷人的”方式来实现这一点。
这就是我如何解决我的问题:
git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD git push
在此,我们基本上正在试图在以前的任务中重写该特定文件的历史。
有关更多信息,您可以在此处参阅过滤器分支的男性页面。
来源:从存储库中删除敏感数据 - 使用过滤器分支
来源:Git:如何删除错误的大文件
git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cached
git commit -am "Remove ignored files"
这取消了被忽略的文件列表,从指数中删除它们,并执行了变更。
使用 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 历史。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别