1

My home directory is a git repository. But to allow me to freely create/delete/test code without cluttering up git status, my .gitignore has an explicit ignore all:

[svenglar@my_box ~]$ cat ~/.gitignore
## Ignore everything by default.
## Use 'git add --force <file>' to add a file/dir.
*

I assume that that means when I create a new directory, it will not be included in the repository. But that doesn't seem to be the case:

[svenglar@my_box ~]$ git rev-parse --is-inside-work-tree
true
[svenglar@my_box ~]$ mkdir test
[svenglar@my_box ~]$ cd test
[svenglar@my_box ~/test]$ git rev-parse --is-inside-work-tree
true
[svenglar@my_box ~/test]$ git status
# On branch master
nothing to commit, working directory clean

I see that git status is clean, but git rev-parse --is-inside-work-tree thinks the new directory is part of the repository.

So my question is, how can I configure git to not consider new directories to be part of the working tree?

--UPDATE--

The reason I'm asking this in the first place is because I use .git-prompt.sh to modify BASH's prompt when I'm working with my git repos. I have multiple projects (folders) under my home folder that are git repos. When I cd to one of those directories, I like to see which branch is active, etc...

But I don't need/want to use .git-prompt.sh when I'm in the "home" repository because I don't consider that a "work" directory.

My fix is to modify .git-prompt.sh with the following check before the last "printf" command:

 # Only modify $PS1 if we are not in the home repository.
 [[ $(git rev-parse --show-toplevel) =~ "${HOME}/" ]] && printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"

I was previously only checking if pwd contained ${HOME}/ (the last forward slash meaning I was in a subdirectory).

Thanks for all your answers. They helped me understand why I was wrong.

svenglar
  • 157
  • 2
  • 8

3 Answers3

1

You cannot.

Use strace git rev-parse --is-inside-work-tree to see what it is doing. If deep inside the tree you see many of these:

chdir("..")                             = 0
stat(".git", 0x7fff22ababa0)            = -1 ENOENT (No such file or directory)

Git just looks for a .git dir all the way up the file system. If it finds one, you are inside a working tree.

Instead of excluding things from your git, you can just set up your working tree in a dedicated subdirectory and use symlinks to explicitly add things you want to track.

michas
  • 22,506
  • 9
  • 64
  • 105
0

Git thinks everything is inside the working tree. .gitignore simply tells git what files to ignore when committing. Your better off not using your home directory as your repo root. Instead, use a sub-directory.

Matt Houser
  • 28,384
  • 5
  • 53
  • 70
0

You can't. And that makes sense.

Any directory under .git is considered to be part of the project. Otherwise, how would you work with source code at all?

If you really don't want that, have the git repository as a bare outside your $HOME, set the GIT_DIR in your environment; then set GIT_WORK_TREE to your $HOME when you want to work with it.

fge
  • 110,072
  • 26
  • 223
  • 312