10

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.

4

4 回答 4

6

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.

于 2012-11-11T22:49:20.017 回答
3

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
于 2012-06-20T21:37:11.177 回答
3

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".

于 2012-11-11T22:05:48.983 回答
1

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).

于 2016-11-02T14:57:31.127 回答