Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I gitignore files in a directory structure but maintain the directory structure?

Tags:

git

Let's assume I have a dir structure like so:

app/
  uploads/
    .gitkeep
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

I want to keep the dir structure but not include the files within (except .gitkeep obviously). The docs say:

A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

So, I would expect this to do the trick:

/app/uploads/**
!/app/uploads/**/.gitkeep

This isn't working however. None of the subdirectories are being added.

like image 536
Jbird Avatar asked Feb 27 '14 23:02

Jbird


People also ask

Does .gitignore work in subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I ignore an entire directory in git?

Repository exclude - For local files that do not need to be shared, you just add the file pattern or directory to the file . git/info/exclude .

Where should Gitignore file be placed?

gitignore is located in the root directory of your repo. / will ignore directories with the name.

How do I ignore files in my repository?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


1 Answers

The structure that I'm used, looks like this:

app/
  .gitignore
  uploads/
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

Content of my app/.gitignore file:

uploads/**    # Ignore everything in 'uploads' folder
!uploads/**/  # exclude subdirectories
!.gitkeep     # and .gitkeep files.
like image 103
Pantelis Peslis Avatar answered Sep 24 '22 00:09

Pantelis Peslis