Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 255
iconoclast Avatar asked Jun 20 '12 21:06

iconoclast


People also ask

How does git keep track of its versions?

Every version of your data is kept in the object database, which lives in the subdirectory . git/objects ; the other residents of . git/ hold lesser data: the index, branch names, tags, configuration options, logs, the current location of the head commit, and so on.


1 Answers

You can do exactly what you want if you first initialize a git repository for the file:

git init --bare .foo.txt

and then run git like this:

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

Alias the first part of that second command to git-foo and you're off to the races.

like image 53
wjl Avatar answered Oct 06 '22 00:10

wjl