我们遇到了这样一种情况,我们的 repo 协议(protocol)中的文件应该被锁定在适当的位置,无法更改。
我已经将文件添加到 .gitignore
但是当我在本地修改它时 git 仍然跟踪它——我现在的理解是将文件/文件夹添加到 .gitignore 之后它们已经在repo 不起作用,一种可能性是使用 git rm --cached file
取消跟踪它们。
如果我这样做,该文件是否会在下一个克隆它的人的 repo 协议(protocol)中不再可用?有没有更好的方法在 repo 中卡住这个文件?
最佳答案
配置您自己的存储库并不难,因此他们永远不会提交对该路径的更改,其他存储库的所有者将不得不合作以执行一些一次性配置命令。
以下是您为自己的存储库所做的事情:
设置一个不会改变工作树或存储库中现有内容的内容过滤器。
git config filter.pinned-content.clean 'git show HEAD:%f 2>&- || cat' git config filter.pinned-content.smudge 'cat %f 2>&- || cat'
每当您暂存 (
git add
) 此类文件时,git 将执行clean
命令以生成要放入 repo 中的内容。在这里,命令尝试显示已为文件提交的内容,如果这不起作用,如果没有提交任何内容,那么它们将获取您正在暂存的内容。同样,git 执行smudge
命令以生成应在 checkout 时放入工作树中的内容,此处使用原样使用的内容,否则默认使用存储库中的内容。通告您希望以这种方式处理路径/到/文件
echo path/to/file filter=pinned-content >>.gitattributes
使处理适用于此 repo 协议(protocol)中所有 future 的 checkout /添加,即使是已经存在的提交
mkdir -p .git/info echo path/to/file filter=pinned-content >> .git/info/attributes
这是分发合作请求和说明的一种方法:
# commit the new .gitattributes and explain what you need by way of cooperation:
git add .gitattributes
git commit -F- -- .gitattributes <<EOD
How to set up to preserve existing file content
# set up a content filter that will not alter existing content
# either in the worktree or in the repository:
git config filter.pinned-content.clean 'git show HEAD:%f 2>&- || cat'
git config filter.pinned-content.smudge 'cat %f 2>&- || cat'
# make the treatment apply to all future checkouts/adds in this repo
mkdir -p .git/info
echo path/to/file filter=pinned-content >> .git/info/attributes
-------
Please do the above configuration to avoid mistakenly committing local changes
to files marked with the `pinned-content` filter. Currently path/to/file is
marked in the committed `.gitattributes` to be treated this way, you can locally
mark files to be treated this way for even preexisting commits by adding a
similar line to your repository's `.git/info/attributes` file.
EOD
%f
功能已包含在 1.7.4 中,所以已经四年了,期待发行版提供该功能似乎是合理的。
您可以使用 git 的稀疏 checkout 获得非常相似的效果,但它不会处理提供默认内容,也无法通过 .gitattributes
启用。
这已经通过了冒烟测试和几个小时的反复试验,看起来不太像我第一次尝试。
关于git - 如何卡住存储库中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23528104/