Git - 如何删除 push 过一次的文件链路追踪?

(以 target 文件夹为例)如果你已经在 .gitignore 中添加了 target/ 目录,但 target 文件夹仍然出现在 Git 的变更列表中,可能是因为它之前已经被添加到 Git 仓库中。即使你更新了 .gitignore,Git 仍然会跟踪这些文件。要彻底解决这个问题,你可以按照以下步骤操作。

1. 确认 .gitignore 文件中正确添加了 target/

确保你的 .gitignore 文件包含以下行:

bash 复制代码
/target/

这会忽略根目录下的 target 文件夹和所有子文件夹中的内容。

2. 从 Git 缓存中移除已经被跟踪的 target 文件

如果 target 文件夹已经被 Git 跟踪,需要先从 Git 的暂存区中移除这些文件,执行以下命令:

bash 复制代码
git rm -r --cached target/

这条命令不会删除本地的 target 文件夹或其内容,它只会从 Git 的跟踪列表中移除这些文件。

3. 提交更改

在移除 target 文件夹的跟踪之后,提交更改:

bash 复制代码
git add .gitignore
git commit -m "Remove target folder from tracking"

4. 推送到远程仓库

将更改推送到远程仓库:

bash 复制代码
git push

总结

这一步骤通过将 target/ 文件夹从 Git 缓存中移除,并确保 .gitignore 正确忽略它,从而彻底屏蔽该文件夹的提交。

相关推荐
是代码侠呀41 分钟前
从前端视角看网络协议的演进
leetcode·开源·github·github star·github 加星
yuanbenshidiaos1 小时前
讲讲git 和svn
git·svn
刃神太酷啦3 小时前
类和对象(1)--《Hello C++ Wrold!》(3)--(C/C++)
java·c语言·c++·git·算法·leetcode·github
互联网搬砖老肖3 小时前
Web 架构之故障自愈方案
前端·架构·github
江鸟19984 小时前
AI日报 · 2025年05月11日|传闻 OpenAI 考虑推出 ChatGPT “永久”订阅模式
人工智能·gpt·ai·chatgpt·github
s9123601015 小时前
Git 用法总结
git
江边垂钓者14 小时前
Git简介和发展
git
老马啸西风16 小时前
sensitive-word-admin v2.0.0 全新 ui 版本发布!vue+前后端分离
vue.js·ui·ai·nlp·github·word
是代码侠呀17 小时前
飞蛾扑火算法matlab实现
开发语言·算法·matlab·github·github star·github 加星
大卫小东(Sheldon)19 小时前
GIM: 调用AI自动生成git提交消息的工具
git·rust