How do I prevent git from thinking new directories are part of the working tree?

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
[svenglar@my_box ~]$ mkdir test
[svenglar@my_box ~]$ cd test
[svenglar@my_box ~/test]$ git rev-parse --is-inside-work-tree
[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?


The reason I’m asking this in the first place is because I use 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 when I’m in the “home” repository because I don’t consider that a “work” directory.

My fix is to modify 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.

  • restore windows command prompt tools after git bash replace them with unix tools
  • Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args
  • X session in Git Bash on Windows?
  • Git Clone is too slow
  • How come npm install doesn't work on git bash
  • Open git file with specific branch on Github from terminal
  • Non-Terminating Process Git Bash (C#)
  • Configuring git client for multiple git servers
  • 3 Solutions collect form web for “How do I prevent git from thinking new directories are part of the working tree?”

    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.

    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.

    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.

    Git Baby is a git and github fan, let's start git clone.