1

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.

Vinicius Dantas
  • 165
  • 1
  • 10

2 Answers2

1

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

Michael
  • 8,362
  • 6
  • 61
  • 88
jthill
  • 55,082
  • 5
  • 77
  • 137
  • I've tried the second alternative, but it did not work. I still have the same result when accessing deeper folders, e.g.: viniciusd at Viniciuss-MacBook-Pro in ~ on master \ ☿ cd Desktop \ viniciusd at Viniciuss-MacBook-Pro in ~/Desktop on master \ ☿ \ – Vinicius Dantas Nov 28 '14 at 01:02
  • If you're in a directory with no `.git` anywhere on the path to the root, then you have an explicit gitdir override somewhere. Do `echo ${!GIT_*}` to see whether the environment is overriding any .git settings. – jthill Nov 28 '14 at 01:08
  • It is not recognized: ☿ echo ${!GIT_*} zsh: event not found: GIT_ – Vinicius Dantas Dec 04 '14 at 20:57
  • My bad, that's a bashism for "substitute every variable name beginning `GIT_`". – jthill Dec 04 '14 at 21:09
0

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.

Community
  • 1
  • 1
Ajay
  • 2,976
  • 2
  • 28
  • 35