Git取消配置文件的跟踪

一般我们都是通过.gitignore文件来现实commit时不提交一编译中间文件。但.gitignore有个缺点,即当之前该文件已经被跟踪(track),那么再把这个文件名添加到.gitignore中就没有用了。

不过网上有很多文章说,通过以下命令让.gitignore生效:

git rm -rf --cache xxx.h

但执行完该命令,再commit的时候发现remote上的该文件被删除了。如果我们需要继续保留该文件,只是不提交当前的change,该怎么办了?比如应用的一些配置文件,初始化的配置文件需要当然要在remote上存在的,但不希望我们个人的配置信息push到remote上。

此时,就需要以下命令来实现了(暂时取消config.ini跟踪):

认为未改变

git update-index --assume-unchanged config.ini

取消认为未改变

git update-index --no-assume-unchanged config.ini

附:

$ git update-index -help
usage: git update-index [<options>] [--] [<file>...]

    -q                    continue refresh even when index needs update
    --ignore-submodules   refresh: ignore submodules
    --add                 do not ignore new files
    --replace             let files replace directories and vice-versa
    --remove              notice files missing from worktree
    --unmerged            refresh even if index contains unmerged entries
    --refresh             refresh stat information
    --really-refresh      like --refresh, but ignore assume-unchanged setting
    --cacheinfo <mode>,<object>,<path>
                          add the specified entry to the index
    --chmod (+/-)x        override the executable bit of the listed files
    --assume-unchanged    mark files as "not changing"
    --no-assume-unchanged
                          clear assumed-unchanged bit
    --skip-worktree       mark files as "index-only"
    --no-skip-worktree    clear skip-worktree bit
    --info-only           add to index only; do not add content to object database
    --force-remove        remove named paths even if present in worktree
    -z                    with --stdin: input lines are terminated by null bytes
    --stdin               read list of paths to be updated from standard input
    --index-info          add entries from standard input to the index
    --unresolve           repopulate stages #2 and #3 for the listed paths
    -g, --again           only update entries that differ from HEAD
    --ignore-missing      ignore files missing from worktree
    --verbose             report actions to standard output
    --clear-resolve-undo  (for porcelains) forget saved unresolved conflicts
    --index-version <n>   write index in this format
    --split-index         enable or disable split index
    --untracked-cache     enable/disable untracked cache
    --test-untracked-cache
                          test if the filesystem supports untracked cache
    --force-untracked-cache
                          enable untracked cache without testing the filesystem
### 配置 Git 忽略特定文件的方法 #### 创建 `.gitignore` 文件 `.gitignore` 是一个用于指定哪些文件或目录应该被 Git 忽略的配置文件。它通常位于项目的根目录下,可以通过以下方式创建: ```bash touch .gitignore ``` 此命令会在当前工作目录中创建一个新的 `.gitignore` 文件。 --- #### 编辑 `.gitignore` 文件 编辑 `.gitignore` 文件以定义需要忽略的文件模式。以下是常见的规则及其含义: - **单个文件** 如果要忽略某个具体的文件,可以直接写出其路径和名称。例如: ``` /path/to/file.txt ``` - **扩展名匹配** 要忽略具有某种扩展名的所有文件,可以使用通配符 `*`。例如: ``` *.log ``` 这条规则会忽略所有扩展名为 `.log` 的文件[^1]。 - **整个目录** 若要忽略整个目录,可以在目录名后面加上斜杠 `/`。例如: ``` /logs/ ``` 此规则表示忽略项目中的 `logs/` 目录以及其中的内容[^2]。 - **排除已忽略项** 可以通过前缀感叹号 `!` 来取消某些忽略规则。例如: ``` # 忽略所有的 .txt 文件 *.txt # 忽略 special.txt 文件 !special.txt ``` - **多级目录支持** 使用双星号 `**` 表示任意级别的子目录。例如: ``` **/*.tmp ``` 上述规则会忽略任何位置下的 `.tmp` 文件[^3]。 --- #### 提交 `.gitignore` 到版本库 完成 `.gitignore` 文件编写后,将其提交Git 版本控制系统中: ```bash git add .gitignore git commit -m "Add .gitignore file" ``` 这一步非常重要,因为只有当 `.gitignore` 文件本身也被跟踪时,其他开发者克隆该项目时才能自动应用这些忽略规则。 --- #### 检查忽略规则的效果 为了验证 `.gitignore` 是否生效,可运行以下命令查看状态: ```bash git status ``` 如果目标文件未显示在待提交列表中,则说明该文件已被成功忽略。另外,也可以利用 `git check-ignore` 命令定位具体哪一条规则导致某文件被忽略。例如: ```bash git check-ignore -v path/to/App.class ``` 上述命令可能会返回如下结果,表明第 3 行的规则 `*.class` 导致了 `App.class` 被忽略: ``` .gitignore:3:*.class App.class ``` --- #### 示例:完整的 `.gitignore` 文件 下面是一个典型的 `.gitignore` 文件内容示例,适用于 Java 开发环境: ``` # 忽略编译生成的 class 文件 *.class # 忽略 IDE 自动生成的临时文件 .idea/ .vscode/ # 忽略日志文件 *.log # 忽略构建工具生成的目录 /target/ /build/ # 排除特定文件被忽略 !/important.log ``` --- ### 注意事项 - 已经被追踪的文件会因后续修改 `.gitignore` 而停止追踪。若想让此类文件再受版本控制影响,需先移除它们再重新添加忽略规则。例如: ```bash git rm --cached file-to-ignore.txt ``` - 确保团队成员共享相同的 `.gitignore` 设置,以免同开发者的本地环境差异引发冲突[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值