How to track directories but not their files with Git?

安稳与你 提交于 2019-12-28 03:31:10

问题


I've recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

For example the site I'm working on allows uploads. I want to track the uploads directory so that it is created when branching, etc. but obviously not the files within it (test files whilst in develop branch or the real files in master).

In my .gitignore I have the following:

uploads/*.*

Have also tried (which ignores the whole directory):

uploads/

This directory may also contain sub directories (uploads/thumbs/ uploads/videos/) I would like to be able to track these but not their files.

Is this possible with Git? I've searched everywhere without finding an answer.


回答1:


Git doesn't track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignore file looks something like this:

upload/*

You can do this:

$ touch upload/.placeholder
$ git add -f upload/.placeholder

If you forget the -f you'll see:

$ git add upload/.placeholder
The following paths are ignored by one of your .gitignore files:
upload
Use -f if you really want to add them.
fatal: no files added

Then when you do git status you'll see:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#   new file:   upload/.placeholder
#

Obviously you can then do:

$ touch upload/images/.placeholder
$ git add -f upload/images/.placeholder



回答2:


I wrote about this here.

Add a .gitignore within the directory.




回答3:


Best answerd I've found is to include a .gitignore file in your upload folder with this content

# Ignore everything in this directory
*
# Except this file
!.gitignore

Here you have How can I add an empty directory to a Git repository?




回答4:


The best solution so far:

1) Create a .gitignore file

2) Write inside:

*
*/
!.gitignore

3) Add the .gitignore file to the folder that you want.

Source: https://stackoverflow.com/a/5581995/2958543




回答5:


In order to track only directories but not the files, I did the following. Thanks to the @PeterFarmer's comment on git tracking only files, I've been able to keep all directories excluding the files as described in below.

# exclude everything in every folder
/data/**/*.*

# include only .gitkeep files
!/data/**/*.gitkeep

Adding this to the .gitignore file will do the work. The following is my folder structure.

data/
├── processed
│   ├── dataset1.csv
│   └── dataset2.csv
├── raw
│   ├── raw_dataset1.json
└── test
    ├── subfolder
    │   └── dataset2.csv
    └── reviews.csv

When I do git add . && git status, git only recognizes folders, but not files.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        new file:   data/processed/.gitkeep
        new file:   data/raw/.gitkeep
        new file:   data/test/.gitkeep
        new file:   data/test/subfolder/.gitkeep

Keep in mind that the following for your .gitignore files:

Prepending slash looks only for the root directory.

/dir

Double asterisk looks for zero or more directories.

/**/



来源:https://stackoverflow.com/questions/5091017/how-to-track-directories-but-not-their-files-with-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!