将文件保存在 Git 存储库中,但不跟踪更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9794931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:15:37  来源:igfitidea点击:

Keep file in a Git repo, but don't track changes

git

提问by gorelative

I have several files in a CodeIgniter site that I will want to have in the repo but not track any changes on.

我在 CodeIgniter 站点中有几个文件,我希望在 repo 中拥有这些文件,但不跟踪任何更改。

For example, I deploy a new installation of this framework to a new client, I want the following files to be downloaded (they have default values CHANGEME) and I just have to make changes specific to this client (database credentials, email address info, custom CSS).

例如,我将此框架的新安装部署到新客户端,我希望下载以下文件(它们具有默认值 CHANGEME),我只需要针对此客户端进行更改(数据库凭据、电子邮件地址信息、自定义 CSS)。

// the production config files i want the files but they need to be updated to specific client needs
application/config/production/config.php
application/config/production/database.php
application/config/production/tank_auth.php
// index page, defines the environment (production|development)
/index.php
// all of the css/js cache (keep the folder but not the contents)
/assets/cache/*
// production user based styling (color, fonts etc) needs to be updated specific to client needs
/assets/frontend/css/user/frontend-user.css

Currently if I run

目前如果我跑

git clone [email protected]:user123/myRepo.git httpdocs

and then edit the files above, all is great. Until I release a hotfix or patch and run git pull. All of my changes are then overwritten.

然后编辑上面的文件,一切都很好。直到我发布修补程序或补丁并运行git pull. 然后我的所有更改都将被覆盖。

回答by nasirkhan

git has a different solution to do this. First change the file you do not want to be tracked and use the following command:

git 有一个不同的解决方案来做到这一点。首先更改您不想被跟踪的文件并使用以下命令:

git update-index --assume-unchanged FILE_NAME

and if you want to track the changes again use this command:

如果您想再次跟踪更改,请使用以下命令:

git update-index --no-assume-unchanged FILE_NAME

git-update-index documentation

git-update-index 文档

回答by Anthony

To pull an answer out of the comments of another answer:

从另一个答案的评论中提取答案:

$ git update-index --skip-worktree FILENAME

$ git update-index --skip-worktree FILENAME

Appears to be a better option than --assume-unchanged

似乎是一个更好的选择 --assume-unchanged

More can be read about this here: Git - Difference Between 'assume-unchanged' and 'skip-worktree'

可以在此处阅读有关此内容的更多信息:Git - “假设未更改”和“跳过工作树”之间的区别

回答by superiggy

For my Code Igniter projects, I keep database.php.exampleand config.php.examplein the repo.

对于我的 Code Igniter 项目,我将database.php.exampleconfig.php.example保留在 repo 中。

Then I add config.phpand applications/config/database.phpto the .gitignorefile.

然后我将config.phpapplications/config/database.php 添加.gitignore文件中。

So, finally, when I deploy, I can copy the .example files (to config.php and database.php), customize them, and they won't get tracked by GIT.

所以,最后,当我部署时,我可以复制 .example 文件(到 config.php 和 database.php),自定义它们,并且它们不会被 GIT 跟踪。

回答by Hrvoje Mati?

The answers given are solving the problem just partially, but introducing even more problems.

给出的答案只是部分解决了问题,但引入了更多问题。

I needed to preserve the"Web.Local.config" file. The only solution that worked for me was to:

我需要保留“Web.Local.config”文件。对我有用的唯一解决方案是:

1. Exclude the file from the project

1. 从项目中排除文件

2. Create a separate copy of the file as Web.LocalTemplate.config

2. 创建一个单独的文件副本作为 Web.LocalTemplate.config

Now that "Web.LocalTemplate.config" is tracked by Git, all developers can use it as a starting point. However, the "Web.Local.config", not being part of the project will be automatically ignored.

现在“Web.LocalTemplate.config”已被 Git 跟踪,所有开发人员都可以使用它作为起点。但是,不属于项目的“Web.Local.config”将被自动忽略。

回答by Michael Durrant

I would put them in your .gitignorefile and just copy them manually as needed.

我会将它们放入您的.gitignore文件中,然后根据需要手动复制它们。

So the skeleton filename would be in git, the ignore filenames would not be in git (but would be in .gitignore). That way, when the manual step to copy them to 'actual' from 'template' (better name than skeleton perhaps) is done they are immediately ignored.

所以骨架文件名将在 git 中,忽略文件名将不在 git 中(但将在 .gitignore 中)。这样,当将它们从“模板”(可能比骨架更好的名称)复制到“实际”的手动步骤完成时,它们会立即被忽略。