iconoclast iconoclast - 1 month ago 4
Git Question

How can I use Git to track versions of a single file?

Let's say I have a single file, foo.txt, that I want to manage with Git, and I don't want to move it into its own directory out of (let's say) /Users/me/Documents, which also has a ton of other files I don't want to include in a repo with foo.txt. Is this possible?

I tried creating a bare repo (so it would not be named .git):

git init --bare .foo.txt


and planned to exclude everything except foo.txt by putting something like this in the info/exclude file:

*
!foo.txt


but the problem I ran into is that I can't even do a
git status
as there seems to be no way to pass in a non-standard replacement for the .git directory.

Using a normal .git directory would not work, because it would limit me to ever versioning that one file, or force me to include any other files in the same repo, and I want to be able to version other files in that directory in their own repo.

I realize this is not the use case Git is designed for, but since it's a powerful tool that I'm already familiar with, I'd prefer to use it rather than something else, if there's a way to do that.

wjl wjl
Answer

You can do exactly what you want if you run git like this:

git --git-dir=.foo.txt --work-tree=. status

Alias the first part to git-foo and you're off to the races.

Comments