[英]How do I stop Git from tracking any changes to a file from this commit forward?
I have a database configuration file that has default values that are unimportant. 我有一个数据库配置文件,其默认值不重要。 However, any changes to this file would contain sensitive information that should not be tracked in the repo. 但是,对此文件的任何更改都将包含不应在repo中跟踪的敏感信息。
I would like future pulls of the Git repository to include the default version but disregard any changes made by any user. 我希望Git存储库的未来可以包含默认版本,但忽略任何用户所做的任何更改。
The following keeps a local configuration but pushes the delete to the repo resulting in issues for future pulls. 以下内容保留了本地配置,但将删除推送到repo导致将来拉出的问题。
cat "app/dir/config.file" >> .gitignore
git rm --cached app/dir/config.file
The following does the job but does not persist past the push to the repo. 以下是这项工作,但不会持续超过对回购的推动。
git update-index --assume-unchanged app/dir/config.file
This seems like a common requirement for version control around sensitive information but I can't seem to find a solution. 这似乎是围绕敏感信息进行版本控制的常见要求,但我似乎无法找到解决方案。
As usual github has a great doc on this. 和往常一样,github有一个很棒的文档。
https://help.github.com/articles/ignoring-files#ignoring-versioned-files https://help.github.com/articles/ignoring-files#ignoring-versioned-files
Here's the relevant snippet: 这是相关的片段:
Ignoring versioned files 忽略版本化文件
Some files in a repository change often but are rarely committed. 存储库中的某些文件经常更改,但很少提交。 Usually, these are various local configuration files that are edited, but should never be committed upstream. 通常,这些是编辑的各种本地配置文件,但绝不应在上游提交。 Git lets you ignore those files by assuming they are unchanged. Git允许您通过假设它们不变来忽略这些文件。
- In Terminal, navigate to the location of your Git repository. 在终端中,导航到Git存储库的位置。
- Run the following command in your terminal: 在终端中运行以下命令:
git update-index --assume-unchanged path/to/file.txt
Once you mark a file like this, Git completely ignores any changes on it. 一旦你标记了这样的文件,Git就完全忽略了它的任何变化。 It will never show up when running
git status
orgit diff
, nor will it ever be committed. 它在运行git status
或git diff
时永远不会出现,也不会被提交。To make Git track the file again, simply run: 要让Git再次跟踪文件,只需运行:
git update-index --no-assume-unchanged path/to/file.txt
.git update-index --no-assume-unchanged path/to/file.txt
。
Not sure if this is the "best" way... but what I have found to work takes a few steps. 不确定这是否是“最佳”方式......但我发现工作需要几步。
New Laravel Example: 新Laravel示例:
git init
.gitignore
files for desired results like not losing the vendors folders. 调整.gitignore
文件以获得所需的结果,例如不丢失供应商文件夹。 git add .
git commit -m "first commit"
git rm --cached public /.htaccess
then git rm --cached .env
git rm --cached public /.htaccess
然后git rm --cached .env
git commit
git status
should show those files as deleted. git status
应该将这些文件显示为已删除。 Now those can be edited, and you can have local and production versions. 现在可以编辑它们,您可以拥有本地版本和生产版本。 NOTE: You may have to repeat all these steps the first time on the production set-up as well. 注意:您可能必须在生产设置上第一次重复所有这些步骤。 So far this has worked well for me. 到目前为止,这对我来说效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.