How to use git to track single file versions?

Let's say I have one foo.txt file that I want to work with Git with, and I don’t want to move it to my own directory from (let them say) / Users / me / Documents, which also has tons of other files that I don't want to include in repo with foo.txt. Is it possible?

I tried creating a bare repo (so it won't be called .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 make git status , since there seems to be no way to pass in a non-standard replacement for the .git directory.

Using the regular .git directory will not work because it will ever limit me to the version of this single file or force me to include any other files in the same repo, and I want to be able to release other files in this directory in my own repo .

I understand that this is not the use case that is intended for w92, but since it is a powerful tool that I am already familiar with, I would rather use it than anything else if there is a way to do that.

+7
source share
4 answers

You can do exactly what you want if you run git as follows:

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

The alias of the first part of git-foo , and you go to the race.

+6
source

How to use a .gitignore file to create a filter for files that need to be edited. This solution avoids hard links that are not supported on Windows.

For example, the following file:

 $ cat .gitignore * !data.xml 

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

+3
source

I believe that it is best to make a repo in a separate directory and create a hard link inside it for each file that you want to include:

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

I find it most convenient to track a single file in a repository stored in a separate directory.

Make directory to store git repository

 mkdir my-repo-path 

Go to the worktree directory (where the file is located)

 cd my-worktree-path 

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

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

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

 del /AH .git 

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

 * !foo.txt 

At a command prompt, go to the git repository and add what will be done.

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

Add a remote start and click the leading branch, adding a link up (tracking) 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 from the second to the last step).

+1
source

All Articles