如果我这样做会不会导致下一个克隆它的人无法访问该文件?还有更好的冻结此文件的方法吗?
git update-index --assume-unchanged somefile
。git update-index --no-assume-unchanged somefile
可以将其恢复。 - Eneko Alonso配置自己的代码库,使其不会对某个路径进行更改并不难,但其他代码库的所有者需要合作,执行一些一次性的配置命令。
以下是为自己的代码库所需执行的步骤:
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'
git will execute the clean
commands to generate the content to be put in the repo whenever you stage (git add
) such files. Here, the commands try to show what's already committed for the file, and if that doesn't work, if nothing has been committed for it, then they'll take what you're staging. Likewise, git executes the smudge
commands to generate the content that should be put in the worktree on checkout, here using whatever's already there as-is, otherwise taking the content from the repository as a default.
advertise that you want path/to/file treated this way
echo path/to/file filter=pinned-content >>.gitattributes
make the treatment apply to all future checkouts/adds in this repo, even of already-existing commits
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的稀疏检出实现非常相似的效果,但无法处理提供默认内容并且不能通过 .gitattributes
启用。
这已经通过了烟雾测试和数小时的测试,看起来不像我第一次尝试它的样子。
根据这个,最初的回答是这样的:
# Did some change in any file in the local repo
$ git commit anyFile
$ git update-index --skip-worktree myFile
$ git ls-files -v myFile
S myFile
$ git push
而且最棒的是,在git pull
之后,我甚至不需要在其他计算机上输入git update-index ...
命令。
最初的回答:
git rm --cached file
可以从仓库中移除文件,使其只存在于当前工作目录中的未跟踪文件中。将来的克隆操作将不会包括该文件。
Git 并没有真正提供一种冻结文件防止其被修改的方法。最好的做法是创建一个 pre-commit 钩子,在提交之前重置文件,或者最好的做法是在您自己重置文件之前取消提交。
*.png
已经在我的.gitignore
中了(某些图片会被转换为PNG格式以便包含在其中),但是仓库中包含原始PNG格式的图片。这没有任何问题,旧的被跟踪,重建生成的被忽略。更改old.png
只需要执行git commit -m "Old image was changed" old.png
命令。如果我想添加一个新的,只需要执行git add -f some-new.png; git commit -m "Some new image"
命令即可。不能通过提及它们来排除已经在仓库中的文件,并且从现在开始永远排除它们也行不通。