简体   繁体   English

如何阻止Git跟踪来自此提交的文件的任何更改?

[英]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允许您通过假设它们不变来忽略这些文件。

  1. In Terminal, navigate to the location of your Git repository. 在终端中,导航到Git存储库的位置。
  2. 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 or git diff , nor will it ever be committed. 它在运行git statusgit 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示例:

  1. git init
  2. Adjust .gitignore files for desired results like not losing the vendors folders. 调整.gitignore文件以获得所需的结果,例如不丢失供应商文件夹。
  3. Make a copy of the files you don't want tracked (ex: .htaccess, .env, etc) 复制您不想跟踪的文件(例如:.htaccess,.env等)
  4. git add .
  5. git commit -m "first commit"
  6. git rm --cached public /.htaccess then git rm --cached .env git rm --cached public /.htaccess然后git rm --cached .env
  7. git commit
  8. git status should show those files as deleted. git status应该将这些文件显示为已删除。
  9. Now put the file you copied back. 现在把你复制的文件放回去。 Since they are the same name as what you told git to remove from tracking, git will now fully ignore those files. 由于它们与您告诉git从跟踪中删除的名称相同,因此git现在将完全忽略这些文件。

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.

相关问题 如何消除提交以及git历史记录中的更改? - How do I eliminate a commit and it's changes from git history? 如何停止跟踪并忽略对 Git 中文件的更改? - How to stop tracking and ignore changes to a file in Git? 如何阻止 git 跟踪我现在在我的 .gitignore 中的文件,但在拉动时不将其从其他机器上删除? - How do I stop git from tracking a file that I now have in my .gitignore but not have it removed from other machines when pulled? 如何在提交树的其他地方应用来自特定 Git 提交的更改? - How do I apply changes from a specific Git commit somewhere else in the commit tree? 如何阻止 git 跟踪文件夹? - How can i stop git from tracking a folder? 在GIT中,如何从包含其他更改的提交中删除文件删除? - In GIT, how do you remove a file delete from a commit which contains other changes? Git如何区分合并提交期间对文件的更改与普通提交中对文件所做的相同更改? - How does Git differentiate changes to a file during a merge commit from the same changes done to the file in a normal commit? 如何在 Git 中停止跟踪文件但允许在本地副本上对其进行“可忽略的”更改? - How to stop tracking file in Git BUT allow “ignorable” changes to it on local copy? 如何合并Git中先前提交的更改(或实现相同的结果)? - How do I merge changes from a previous commit in Git (or achieve the same result)? 如何阻止自己使用'git commit -a'? - How can I stop myself from using 'git commit -a'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM