Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to "freeze" a file in Git?

Tags:

git

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.

like image 321
Suan Avatar asked Jan 17 '11 05:01

Suan


2 Answers

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.

like image 175
Greg Hewgill Avatar answered Sep 17 '22 17:09

Greg Hewgill


use update-index, here are the docs.

Here is a basic example freezing a file foo.rb:

git update-index --assume-unchanged foo.rb

Then, to get it back:

git update-index --no-assume-unchanged foo.rb

Or for simplicity, add it to .gitconfig

freeze = update-index --assume-unchanged
thaw = update-index --no-assume-unchanged
...
git freeze foo.rb
git thaw foo.rb

Note: This answer was originally provided by @TheAmpersand in comments above. I thought it was important enough to formalize.

like image 23
ynkr Avatar answered Sep 21 '22 17:09

ynkr