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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With