如何阻止 Git 跟踪从此提交向前对文件所做的任何更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18276951/
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
How do I stop Git from tracking any changes to a file from this commit forward?
提问by Ben Campbell
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 存储库的未来 pull 包含默认版本,但忽略任何用户所做的任何更改。
The following keeps a local configuration but pushes the delete to the repo resulting in issues for future pulls.
以下保留本地配置,但将删除推送到存储库,从而导致未来拉取的问题。
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.
这似乎是围绕敏感信息进行版本控制的常见要求,但我似乎找不到解决方案。
回答by Rob
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.
- In Terminal, navigate to the location of your Git repository.
- 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. It will never show up when running
git status
orgit diff
, nor will it ever be committed.To make Git track the file again, simply run:
git update-index --no-assume-unchanged path/to/file.txt
.
忽略版本化文件
存储库中的某些文件经常更改但很少提交。通常,这些是经过编辑的各种本地配置文件,但永远不应向上游提交。Git 允许您通过假设它们未更改来忽略这些文件。
- 在终端中,导航到 Git 存储库的位置。
- 在终端中运行以下命令:
git update-index --assume-unchanged path/to/file.txt
一旦你像这样标记一个文件,Git 就会完全忽略它的任何更改。它永远不会在运行时出现
git status
或git diff
,也永远不会被提交。要让 Git 再次跟踪文件,只需运行:
git update-index --no-assume-unchanged path/to/file.txt
.
回答by bikermusician
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
- Adjust
.gitignore
files for desired results like not losing the vendors folders. - Make a copy of the files you don't want tracked (ex: .htaccess, .env, etc)
git add .
git commit -m "first commit"
git rm --cached public /.htaccess
thengit rm --cached .env
git commit
git status
should show those files as deleted.- 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 init
- 调整
.gitignore
文件以获得所需的结果,例如不丢失供应商文件夹。 - 复制您不想跟踪的文件(例如:.htaccess、.env 等)
git add .
git commit -m "first commit"
git rm --cached public /.htaccess
然后git rm --cached .env
git commit
git status
应该将这些文件显示为已删除。- 现在把你复制回来的文件放回去。由于它们与您告诉 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.
现在可以编辑这些,并且您可以拥有本地和生产版本。注意:您可能还必须在第一次生产设置时重复所有这些步骤。到目前为止,这对我来说效果很好。