While it seemed to do nothing, it gave no warning or error message. Any ideas?
Asked
Active
Viewed 6,571 times
32
-
12Actually a relevant question; I admit I just did it. I dit it because I wanted to add `.gitignore` but tab completion stopped at `.git`, so by accident I did it. And I was (like you) worried "What did I destroy?". – Jarl Sep 07 '12 at 06:01
-
I did it because I've been using GNU stow to install a `.git/info/exclude` file into a repository that I share with other people, and I wanted to put the stow package that contains this `.git` folder under version control. – Jasha Mar 03 '21 at 06:09
2 Answers
26
Comment from Git source:
/*
* Read a directory tree. We currently ignore anything but
* directories, regular files and symlinks. That's because git
* doesn't handle them at all yet. Maybe that will change some
* day.
*
* Also, we ignore the name ".git" (even if it is not a directory).
* That likely will not change.
*/
Experiment to see what happend if I create a file .git
and try to add it:
(on Windows I cannot create a file .git
when there is already a .git
folder. I also could have created a .git
elsewhere in a sub directory, but wanted to try out --git-dir
and --work-tree
which I haven't used before. After all I am experimenting. This also allows me to show that I can add the git metadata folder as seen below)
git --git-dir="c:/test" init
touch blah
git --git-dir="c:/test" --work-tree="." add .
git --git-dir="c:/test" --work-tree="." status ( shows blah added)
touch .git
git --git-dir="c:/test" --work-tree="." add .git ( no output as usual)
git --git-dir="c:/test" --work-tree="." status ( only blah shown)
So yeah, .git
- be it directory or file, is ignored by git.
And if I do something like below:
git --git-dir="c:/test" --work-tree="c:/test" add c:/test
all the meta files get added.
So again, it is only .git
that is ignored not the git metadata folder (that you set via --git-dir
) as far as I can see.

manojlds
- 290,304
- 63
- 469
- 417
-
-
Nevermind, found it -- https://github.com/git/git/blob/fe9122a35213827348c521a16ffd0cf2652c4ac5/dir.c#L1277 – Ben Burns Mar 26 '14 at 02:01
-
What do you mean by "the git metadata dir"? Isn't the directory you set with `--git-dir` your working directory? – HelloGoodbye Jan 11 '16 at 03:51
-
But what happens if you try to add a file _in_ the `.git` directory? Then the name will not be ".git". – HelloGoodbye Jan 11 '16 at 03:54
18
Short answer: Nothing.
Long answer:
laptop:Projects ctcherry$ mkdir test
laptop:Projects ctcherry$ cd test
laptop:test ctcherry$ git init .
Initialized empty Git repository in /Users/ctcherry/Projects/test/.git/
laptop:test ctcherry$ git add .git
laptop:test ctcherry$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
laptop:test ctcherry$

Chris Cherry
- 28,118
- 6
- 68
- 71