自学内容网 自学内容网

【Git】取消追踪多个文件或目录

场景

  1. 清理:不再希望某些文件被 Git 追踪。
  2. 配置忽略文件:通常配合 .gitignore 文件使用,以便以后这些文件不会被重新添加到索引中。

方法

1. 添加到 .gitignore

将这些文件添加到 .gitignore 文件中,这样 Git 就不会继续追踪这些文件。编辑 .gitignore 文件,添加要忽略的文件或目录。

2. 从暂存区移除

使用 git rm 命令来从暂存区移除这些文件。

示例

1. 编辑 .gitignore 文件

# 忽略所有 .log 文件
*.log

# 忽略所有 .log 文件,important.log 除外
!important.log

# 忽略 temp 目录
temp/

# 忽略 temp 目录,temp/important.txt 除外
!temp/important.txt

2. 从暂存区移除文件或目录

取消追踪并保留文件(仅从暂存区移除),通过 --cached 参数

# 移除所有 .log 文件
git rm --cached *.log

# 移除 temp 目录中的所有文件
git rm --cached -r temp

取消追踪并删除文件(同时从暂存区和工作区移除),通过 --f 参数

# 移除所有 .log 文件
git rm --f *.log

# 移除 temp 目录中的所有文件
git rm --f -r temp

原文地址:https://blog.csdn.net/weixin_43553153/article/details/140233418

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!