Vinicius Dantas
Vinicius Dantas

Reputation: 165

How can I make git work only on the current directory?

I have created a git repository for my dot files, but when I've used git init at my home directory, it seems it is considering all the subdirectories as part of the git as well. I've added the repository via git add remote. The problem is that my bash shows in which branch I actually am, e.g., user at userr in folder on branch. Therefore, when I go to my Desktop (~/Desktop), it is still considered a git directory, I can even use git status there. However, I would like to git consider only the home directory for the repository.

Upvotes: 2

Views: 2803

Answers (2)

jthill
jthill

Reputation: 60255

(edit: the third option below, GIT_CEILING_DIRECTORIES, is probably best)

Option 1

Unless you explicitly tell Git where to find the current repository's gitdir, the only way it has to find it is to search upwards. There really isn't any other choice: you tell it, or it searches upwards.

Closest I can get to what you want is to just break git in those directories by inserting an invalid gitdir reference

echo "gitdir: this isn't part of any containing repository" > $notinthisone/.git

and to tell the containing repository to ignore it:

echo $notinthisone >> .gitignore

following which

$ cd $notinthisone
$ git status
fatal: Not a git repository: this isn't part of any containing repository

and git add from the containing repository will skip it—and if you try to force it, you'll get the error message above.


Option 2

For an alternate way to implement your dotfile repo, you can configure a repository to use any worktree you want. So:

mv .git somewhere/out/of/the/way
cd !$
git config core.worktree $OLDPWD

echo '*'     >> .gitignore
echo '!.*'   >> .gitignore

git add .gitignore
git commit -m 'ignoring un-hidden files'

Option 3

Live and learn. While reading the Git docs, checking for all possible hidden overrides to the .git directory, I encountered this:

GIT_CEILING_DIRECTORIES

This should be a colon-separated list of absolute paths. If set, it is a list of directories that git should not chdir up into while looking for a repository directory. It will not exclude the current working directory or a GIT_DIR set on the command line or in the environment. (Useful for excluding slow-loading network directories.)

So export that and do the same in your .bashrc equivalent, e.g.

export GIT_CEILING_DIRECTORIES=$HOME

and Git will not recurse upwards into your home directory. Then in your home directory

echo '*'   >>.gitignore
echo '!.*' >>.gitignore 

and Git will never recurse upwards into your home directory's repo.

Upvotes: 4

Ajay
Ajay

Reputation: 3086

I think I found the solution from this thread : Git ignore sub folders @mgold 's answer . he is suggesting to add

*/*

to gitignore. It worked for me in windows if you are using a linux based system you might need to alter the regex . I hope this helps.

Upvotes: 0

Related Questions