Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect a file in the git repository from commits after ther first commit?

Tags:

git

I have a tracking.properties file under my Java app classpath, which can change every moment like "seq=10" to "seq=17".

The property file is now put into github repository. I hope that the file exists in the repository so that other developers can clone it and that the file be not pushed by them because the "seq" can change from user to user.

I tried

git rm --cached -f <file>

then add tracking.properties to .gitignore, but it results in deleteing the property file in the git repository. It should remain there and only ONCE-FOR-ALL commit be allowed. Is it possible?

like image 607
linzhixing Avatar asked Jun 06 '13 01:06

linzhixing


2 Answers

If each developer runs this command after cloning the repo

git update-index --skip-worktree tracking.properties

then git will forever after treat the file as if it had not changed locally, even when it has.

If you ever do need to commit local changes to it, you can turn it back to normal like this

git update-index --no-skip-worktree tracking.properties

Yet another alternative is

git update-index --assume-unchanged tracking.properties

Here is a good explanation of the subtle differences between skip-worktree and assume-unchanged. I think skip-worktree is better for you, since it ignores reset --hard and preserves the flag when the upstream version has changed.

like image 183
Klas Mellbourn Avatar answered Oct 30 '22 21:10

Klas Mellbourn


Consider using the "template" pattern for this.

Check the file in as tracking.properties.template and add tracking.properties to your .gitignore. Then, after checking out the repository, developers should copy the template file to tracking.properties and then edit it as they wish. This file will be ignored, and so the file will not show up as dirty and developers will be unlikely to commit changes to it.

This will not prevent the template file from changing, but that could be a good thing -- you might need to make changes to it in the future. The important thing is that local-only changes to tracking.properties are possible without too much confusion.

like image 38
cdhowie Avatar answered Oct 30 '22 21:10

cdhowie