忽略对跟踪文件的更改 [英] Ignore changes to a tracked file

查看:52
本文介绍了忽略对跟踪文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想为开发更改跟踪文件,但保持跟踪版本不变.

I want to change a tracked file for development only, but keep the tracked version unchanged.

此建议的大多数解决方案"

Most "solutions" for this suggest

git update-index --assume-unchanged

但是它完全没用,因为该文件仍会通过签出或重置来更改.有没有一种更好的解决方案,可以在签出和重置命令后继续存在?

but it's totally useless, because the file will still be changed by checkout or reset. Is there a better solution that survives checkout and reset commands?

推荐答案

快速修复

这是我认为您要尝试的操作,更改文件,但在提交时将其忽略.

Quick Fix

This is what I think you're trying to do, change a file, but ignore it when committing.

git update-index --skip-worktree my-file

关于 assume-unchangedskip-worktree 之间的差异,这是一个很好的答案.

Here is a good answer regarding the difference between assume-unchanged and skip-worktree.

如果您尝试将更改合并到my-file中,Git仍会发出警告.然后,您将不得不取消跳过"文件,合并并重新跳过"它.

Git will still warn if you try to merge changes into my-file. Then you will have to "unskip" the file, merge it and "re-skip" it.

git update-index --no-skip-worktree my-file
# merge here
git update-index --skip-worktree my-file

如果您修改文件,然后切换到更改了该文件的分支,也可能会出现问题.您可能需要执行一些花哨的跳过/取消"操作才能解决该问题.

There can also be problems if you modify the file, then switch to a branch where that file has been changed. You may have to do some fancy "skip/unskip" operations to get around that.

从长远来看,您可能希望将本地"更改分离到另一个文件中.例如,如果要更改的文件是配置文件,则创建一个默认"配置文件,然后将其签入存储库.然后,允许第二个可选的替代"配置文件,并将该文件放入您的.gitignore.

In the long term, you probably want to separate your "local" changes into a second file. For example, if the file you want to change is a config file, create a "default" config file that you check into the repository. Then, allow a second "overrides" config file that is optional and put that file in your .gitignore.

然后,在您的应用程序中,读取默认配置文件,然后检查替代文件是否存在.如果是这样,请将该数据与默认文件中的数据合并.

Then, in your application, read the default config file and then check if the overrides file exists. If it does, merge that data with the data from the default file.

此示例用于配置文件,但是如果需要,您可以将该技术用于其他类型的替代.

This example is for a config file, but you can use that technique for other kinds of overrides if needed.

这篇关于忽略对跟踪文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆