我把以前由 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 文件 - 例如,添加一个文件夹,你不想跟踪到.gitignore. git rm -r --cached. - 删除所有跟踪的文件,包括所需和不需要的. 您的代码将是安全的,只要你保存了本地. git 添加. - 所有文件将返回,除了那些在.gitignore.
请点击 @AkiraYamamoto 指向我们正确的方向。
git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cached
git commit -am "Remove ignored files"
这取消了被忽略的文件列表,从指数中删除它们,并执行了变更。
我用 git filter-branch 完成了这一点,我所使用的准确命令从 man 页面上取出:
警告:此将从您的整个历史中删除文件
git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD
这个命令将恢复整个命令历史,在每个命令之前执行 git rm 以便摆脱所指定的文件. 不要忘记在运行命令之前备份它,因为它将失去。
git update-index 为我做工作:
git update-index --assume-unchanged <file>
注意: 这个解决方案实际上是独立的.gitignore 因为 gitignore 仅适用于未追踪的文件。
更新,更好的选择
因为这个答案被发布,一个新的选项已经创建,应该是优先的. 你应该使用 --skip-worktree 这是为修改的跟踪文件,用户不想再承诺,并保持 --assume-unchanged 性能,以防止 git 检查大跟踪文件的状态. 查看 https://stackoverflow.com/a/13631525/717372 更多详细信息...
git update-index --skip-worktree <file>
取消
git update-index --no-skip-worktree <file>
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?