1

I'm working on a linux system with nested git repositories. My file/directory structure is as follows:

~/
    .git/
    .gitignore
    file1
    project/
        .git/
        file2
        file3

In my homedir I have created a repo for my linux config files. The ignore file (~/.gitignore) contains only '*' (exclude everything). I have added files with 'git add -f file'. In a nested repo (project) I have discovered that everything is excluded as well even though the correct '(~/project/).git' directory is updated on commit. I have checked this with the following command:

> cd ~/project
> git add file2 
    The following paths are ignored by one of your .gitignore files:
    file2
    Use -f if you really want to add them.
    fatal: no files added

> git check-ignore -v file2
    ~/.gitignore:1:* file2

The command check-ignore is available as of version 1.8.2 and was motivated by the stackoverflow question: which gitignore rule is ignoring my file.

My question is if there is a way to change the behaviour git regarding ignore files to only process the ignorefiles encountered until the repo is found that git commits to, e.g., stop processing ignorefiles higher up in the directory structure than the first '.git' folder is found (in this case ~/project/.git).

Thank you!

Community
  • 1
  • 1
gospes
  • 3,819
  • 1
  • 28
  • 31
  • One technique that might help in this situation (but doesn't solve the main problem) is for .bashrc to delegate to "source .bashrc-$HOSTNAME" (or similarly for .profile). Then any machine specific config can go in there. – Jonathan Hartley Oct 05 '22 at 12:13

2 Answers2

6

Edit: Modifying the parent repository's ignore file to use /* instead of * does in fact seem to do what you want.

Having said that, the rest of the post still applies:

I wouldn't recommend nesting Git repositories unless they are related, in which case using submodules or many of its alternatives is a good idea.

For versioning my dotfiles, I put all of the files I want to version into a dedicated directory, e.g. ~/dotfiles, which can be safely versioned. Then I symlink the files in ~/dotfiles into my home directory. This has the added benefit that I can "disable" individual files on some of my machines by not linking them.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    I presume the syntax '/*' holds for files only in the directory where the .gitignore file is located. I also have config files in subdirectories though, e.g., all files under ~/.vim/*. I have quite a few config files, for instance, i've distributed by bash configuration over 10 files, so i'd prefer not to symlink them all. Any other alternatives? – gospes Dec 20 '13 at 16:15
  • If you symlink the directory `~/dotfiles/vim` to `~/.vim`, Vim should see all of the files inside the directory. If you want to keep things nested the way they are, you can have `/*` and, e.g., `!/.vim` in your top-level `.gitgnore`. This will ignore everything by default, but will *not* ignore files inside your Vim config directory. – ChrisGPT was on strike Dec 20 '13 at 18:01
3

It seemed I had added "git config --global core.excludesfile '~/.gitignore'" somewhere in my bash configuration files. Having removed this from the bash configuration file and from '~/.gitconfig' solved the problem. I conclude that ignore files are therefore not inherited! :)

gospes
  • 3,819
  • 1
  • 28
  • 31
  • indeed, my `--global` config also had my `~/.gitignore` referenced. how peculiar. perhaps it's a default? not sure. either way, thanks for the tipskie. – cdaringe Jul 16 '16 at 21:52