我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。
当前回答
BFG 专门用于从 Git 存储库中删除不需要的数据,如大文件或密码,因此它有一个简单的旗帜,将删除任何大历史文件(不属于您的当前文件):‘--strip-blobs-bigger-than’
java -jar bfg.jar --strip-blobs-bigger-than 100M
如果你想按名称指定文件,你也可以这样做:
java -jar bfg.jar --delete-files *.mp4
BFG 比 git 过滤器行业快 10 至 1000 倍,通常更容易使用 - 查看完整使用指示和示例,以获取更多详细信息。
来源:库存大小减少
其他回答
使用 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 的問題(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
对于我来说,文件在历史上仍然可用,我首先需要清除添加删除文件的命令: https://gist.github.com/patik/b8a9dc5cd356f9f6f980
下面的例子结合了最后3个命令
git reset --soft HEAD~3
git commit -m "New message for the combined commit"
推破的承诺 如果承诺已推到远程:
git push origin +name-of-branch
您想要跟踪很多文件,或者您更新了您的.gitignore 文件
来源: Untrack 已添加到基于.gitignore 的 Git 存储库
假设你已经添加 / 订购了一些文件到你的 Git 存储库,然后你将它们添加到你的.gitignore 文件;这些文件将仍然存在于你的存储库指数。
步骤1:承诺所有的变化
在进行之前,请确保您的所有更改都完成,包括您的.gitignore 文件。
步骤2:从存储库中删除一切
要清理您的存储库,请使用:
git rm -r --cached .
如果你想先尝试它做什么,添加 -n 或 - 干燥的旗帜来测试事情。
步骤3:阅读一切
git add .
git commit -m ".gitignore fix"
您的存储库很干净:)
将更改推到您的远程,以便看到更改在那里有效。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别