本文介绍了有没有办法“冻结” Git中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于想要开源项目的情况,但是有一个源文件希望发布干净版本,但本地使用单独的版本。 git有一个功能,我可以只提交一次文件,并从现在开始停止在该文件中查找更改?



我尝试将文件添加到.gitignore,但是在我第一次在文件上执行 git add -f git commit 后,我继续编辑它, git status 显示文件已更改。理想的行为是让git不要从现在开始改变这个文件,即使我已经编辑它。



我也会对其他人感兴趣在推送到开源代码库之前,特别是在Git上处理了清理其私有代码/数据的代码库。

您可以使用Git的功能。在需要特殊的未跟踪 local.txt 文件的回购库中运行以下命令:

  git config core.sparsecheckout true 
echo'*'> .git / info / sparse-checkout
echo'!local.txt'>> .git / info / sparse-checkout
git read-tree --reset -u HEAD

这将首先删除现有的 local.txt 文件。但是现在它从Git的角度来看是被忽视的,所以你可以在那里放置一台特定的机器,Git不会为此烦恼。

然后你可以添加和管理从某些其他存储库发布 local.txt 文件。 Git会很高兴跟踪这个文件并将它保存在存储库中,但是在具有上述稀疏检出配置的机器上,Git会忽略本地 local.txt 文件,即使它与存储库中的文件不同。


I'm in a situation where I want to open source my project, however there's a single source file that I want to release a "clean" version of, but use a separate version locally. Does git have a feature where I can just commit a file once, and it stops looking for changes on that file from now on?

I've tried adding the file to .gitignore, but after the first time when I do a git add -f and git commit on the file, and I proceed to edit it again, git status shows the file as changed. The ideal behavior would be for git to not show this file as changed from now on, even though I've edited it.

I'd also be interested in how others have dealt with "scrubbing" their codebases of private code/data before pushing to an open source repo, especially on Git.

解决方案

You can do this with Git's sparse checkout feature. Run the following commands in a repo where you want a special, untracked local.txt file:

git config core.sparsecheckout true
echo '*' >.git/info/sparse-checkout
echo '!local.txt' >>.git/info/sparse-checkout
git read-tree --reset -u HEAD

This will first delete the existing local.txt file. But now it's ignored from Git's perspective, so you can put a machine-specific one there and Git won't bother you about it.

You can then add and manage the published local.txt file from some other repository. Git will be happy to track this file and keep it in the repository, but on a machine with the above sparse checkout configuration, Git will ignore the local local.txt file in that working directory, even if it is different from what's in the repository.

这篇关于有没有办法“冻结” Git中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:33