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

I think your best bet is to make a repo in a separate directory, and create a hardlink inside of it for each file you want to include:

mkdir some_repo
cd some_repo
git init
ln path/to/file file
# add & commit

It's most convenient for me to track a single file in the repository stored in a separate directory.

Make a directory to store the git repository

mkdir my-repo-path

Go to worktree directory (where your desired file is located)

cd my-worktree-path

Init a separate repository inside the hidden subdirectory .git of my-repo-path

git init --separate-git-dir my-repo-path\.git

Along with the separate repository, git has created a hidden file .git in the worktree directory. You can delete it if you like (it may be useful if you are going to track some other file(s) in your worktree directory separately in some separate repository(ies)).

del /AH .git

In any text editor open the file my-repo-path\.git\info\exclude and add to the end the following lines (where foo.txt is the file you'd like to track):

*
!foo.txt

In the command prompt go to the git repo and add what will be committed.

cd my-repo-path\.git
git add .
git commit

Add a remote origin and push master branch, adding upstream (tracking) reference to it.

git remote add origin <url>
git push -u origin master

After that you will add the following commits from your git repo directory (see the second to last step).


How about using a .gitignore file to create a filter of the files that should be edited. This solution avoids hardlinks which are unsupported on windows.

For example the following file:

$ cat .gitignore
*
!data.xml

Configures git to ignore all files except "data.xml".


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.

Tags:

Git