4

We have a deep directory structure with multiple README files at different levels. For instance:

gitignoretest/
|-- 00README
|-- dir1
|   |-- 00README
|   |-- dir1
|   |   |-- 00README
|   |   |-- dir1
|   |   |-- dir2
|   |   |-- dir3
|   |   |-- file1
|   |   `-- file2
|   |-- dir2
|   |   |-- 00README
|   |   |-- dir1
|   |   |-- dir2
|   |   |-- dir3
|   |   |-- file1
|   |   `-- file2
|   |-- dir3
|   |   |-- 00README
|   |   |-- dir1
|   |   |-- dir2
|   |   |-- dir3
|   |   |-- file1
|   |   `-- file2
|   |-- file1
|   `-- file2
|-- dir3
|   |-- 00README
|   |-- dir1
|   |   |-- 00README
|   |   |-- dir1
|   |   |   |-- 00README
|   |   |   |-- dir1
|   |   |   |-- dir2
|   |   |   |-- dir3
|   |   |   |-- file1
|   |   |   `-- file2
|   |   |-- dir2
|   |   |   |-- 00README
|   |   |   |-- dir1
|   |   |   |-- dir2
|   |   |   |-- dir3
|   |   |   |-- file1
|   |   |   `-- file2

...
...

We want to ONLY version the 00README files. We successfully tested this with this .gitignore

.gitignore

# Ignore everything
*

# But not these files...
!.gitignore
!00README
# etc...

# ...even if they are in subdirectories
!*/

However, in the real scenario, one of the subdirectories is a conda installation which has some packages containing their own .git directories. When I do git add . it fails with the message:

fatal: Not a git repository: Applications/conda/lib/STAR-Fusion/STAR-Fusion.wiki/../.git/modules/STAR-Fusion.wiki

The real scenario is ../TopLevel/Applications/... with ../TopLevel/.gitignore. Here are the things I've tried:

../TopLevel/.gitignore

# Ignore everything
*
/Applications/conda/**/*
/Applications/miniconda3/**/*
/Applications/newconda/**/*

Applications/conda/**/*
Applications/miniconda3/**/*
Applications/newconda/**/*

/**/.git
**/.git

/**/.git/**
**/.git/**

# But not these files...
!.gitignore
!00README.md
!USE_APPS
# etc...

# ...even if they are in subdirectories
!*/

But not hitting the mark.


EDIT
To address the issue of "untracking" subdirectories with .git folders as mentioned by @VonC, I present a completely fresh creation of the git repository. Note: the repository being added as the remote is a freshly minted --bare repository.

balter@server:/home/.../TopLevel$ rmdir .git
balter@server:/home/.../TopLevel$ git init
Initialized empty Git repository in /home/.../TopLevel/.git/
balter@server:/home/.../TopLevel$ git remote add origin git@server.ohsu.edu:path/repo.git
balter@server:/home/.../TopLevel$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   00README.md
#   Applications/
#   InstallationPackages/
#   USE_APPS
#   gitignoretest/
nothing added to commit but untracked files present (use "git add" to track)
balter@server:/home/.../TopLevel$ git add .
fatal: Not a git repository: Applications/conda/lib/STAR-Fusion/STAR-Fusion.wiki/../.git/modules/STAR-Fusion.wiki
Community
  • 1
  • 1
abalter
  • 9,663
  • 17
  • 90
  • 145

1 Answers1

1

If you want to consider the all tree structure as one repository, you need to remove the nested .git subfolders first: see "Initial push to GitHub Missing Sub Directory".

Regarding The ignore rules, if you want to ignore everything but the OOREMEADE files, you need fisrt to exclude the folders

*
!**/

Then you can exclude files:

!00README

Make sure those files are untracked first (git status)

Our Applications directory has a conda installation that seems to install many packages by cloning git repositories. If I go and delete all the .git directories then whenever I run conda update, either it will fail because it no longer has the repos it needs, or it will re-clone them.

First, you don't ignore .git folders: their parent folder represents a nested git repo. That is the folder (the parent of the .git folder) you want to ignore, that way a git add . won't record a gitlink (special entry in index of the parent repo)

Second, "Not a git repository: .../../.git/modules/..." means the conda nested repo has submodules, whose support is still in progress with conda: that conda installation folder would need a git submodule update --init --recursive in order to initialize the proper nested submodules.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Trying it. A couple of questions: 1) it "worked" in our test directory, albeit there were no folders that were themselves repositories. 2) if I understand, `!**/` is to ignore anything that is actually a folder, and not a file. Right? – abalter Mar 07 '17 at 19:01
  • @abalter 2) yes: if a folder is ignored, any rule involving an element in that ignored subfolder won't be used at all. 1) What is the question ;) – VonC Mar 07 '17 at 19:02
  • Tried the above, and still got fatal error :( Regarding 1) the question was that in the test directory (tree shown in original question) the `.gitignore` I showed worked fine in terms of ignoring directories. So why is it failing when there is a `.git` directory somewhere down the road? Maybe I need to ignore all directories starting with "."? – abalter Mar 07 '17 at 19:26
  • That is what my first link k details – VonC Mar 07 '17 at 19:28
  • see my edit. Does that fully address the issue of needing to make sure the files are un-tracked? – abalter Mar 07 '17 at 20:28
  • @abalter your edit seems to indicate you still have nested git repo inside your freshly new git repo: remove any .git subfolder (beside the one created by git init) – VonC Mar 07 '17 at 20:52
  • @VonC--oh golly I must not have been clear at all with my original question. Our Applications directory has a conda installation that seems to install many packages by cloning git repositories. If I go and delete all the `.git` directories then whenever I run `conda update`, either it will fail because it no longer has the repos it needs, or it will re-clone them. So there is no way to fix the problem by removing those directories. We need a solution that works with them there. – abalter Mar 08 '17 at 03:39
  • @abalter OK. I have added some information and edited the answer. – VonC Mar 08 '17 at 07:22