如何将空目录添加到Github

在使用git的时候创建一个空文件夹

mkdir aa
git status

发现git根本没发现新建了一个空文件夹,为何git要忽略空文件夹呢?

有的说git开发者不喜欢空文件夹,有的人说空文件夹没有什么实际意义,还有的人说空文件夹的话用某些服务器比如ftp会报错,总之git不支持空文件夹的提交。

解决办法和相关回答:

1、create a .gitignore file

Another way to make a directory stay (almost) empty (in the repository) is to create a .gitignore file inside that directory that contains these four lines:

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

Then you don't have to get the order right the way that you have to do in m104's solution.

This also gives the benefit that files in that directory won't show up as "untracked" when you do a git status.

Making @GreenAsJade's comment persistent:

I think it's worth noting that this solution does precisely what the question asked for, but is not perhaps what many people looking at this question will have been looking for. This solution guarantees that the directory remains empty. It says "I truly never want files checked in here". As opposed to "I don't have any files to check in here, yet, but I need the directory here, files may be coming later".

2、You can't.

You can't. See the Git FAQ.

Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.

Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

You can say "git add <dir>" and it will add files in there.

If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.

3、Create an empty file called .gitkeep in the directory, and add that.

4、You could always put a README file in the directory with an explanation of why you want this, otherwise empty, directory in the repository.

5、create an empty .keep file

touch .keep

On Linux, this creates an empty file named .keep. This name is preferred over .gitkeep as the former is agnostic to Git, whereas the latter is specific to Git. Secondly, as another user has noted, the .git prefix convention should be reserved for files and directories that Git itself uses.

Alternatively, as noted in another answer, the directory can contain a descriptive README or README.md file instead.

Of course this requires that the presence of the file won't cause your application to break.

6、Why would we need empty versioned folders

First things first:

An empty directory cannot be part of a tree under the Git versioning system.

It simply won't be tracked. But there are scenarios in which "versioning" empty directories can be useful, for example:

  • scaffolding a predefined folder structure, and make this structure available to every user/contributor of the repository; or, as a specialized case of the above, creating a folder for temporary files, such as a cache/ or logs/ directories, where we want to provide the folder but .gitignore its contents
  • related to the aboce, some projects won't work without some folders (which is often a hint of a poorly designed project, but it's a frequent real-world scenario and maybe there could be, say, permission problems).

Some suggested workarounds

Many users suggest:

  1. Placing a README file or another file with some content in order to make the directory non-empty, or
  2. Creating a .gitignore file with a sort of "reverse logic" (i.e. to include all the files) which, at the end, serves the same purpose of approach #1.

While both solutions surely work I find them unconsistent with a meaningful approach to Git versioning.

  • Why are you supposed to put bogus files or READMEs that maybe you don't really want in your project?
  • Why use .gitignore to do a thing (keeping files) that is the very opposite of what it's meant for (excluding files), even though it is possible?

.gitkeep approach

Use an empty file called .gitkeep in order to force the presence of the folder in the versioning system.

Although it may seem not such a big difference:

  • You use a file that has the single purpose of keeping the folder. You don't put there any info you don't want to put.

    For instance, you should use READMEs as, well, READMEs with useful information, not as an excuse to keep the folder.

    Separation of concerns is always a good thing, and you can still add a .gitignore to ignore unwanted files.

  • Naming it .gitkeep makes it very clear and straightforward from the filename itself (and also to other developers, which is good for a shared project and one of the core purposes of a Git repository) that this file is

    • A file unrelated to the code (because of the leading dot and the name)
    • A file clearly related to Git
    • Its purpose (keep) is clearly stated and consistent and semantically opposed in its meaning to ignore

Adoption

I've seen the .gitkeep approach adopted by very important frameworks like LaravelAngular-CLI.

 

我的做法:

我是在空文件夹下面新建一个 .gitkeep 文件,里面没有写什么,内容为空。

在ecplise中,ecplise会自动忽略点后缀为gitkeep的文件,即使看不到点不着 .gitkeep 文件,我们也可以从图标也可以看出来有这个文件,包前面会多出一个小角,可以点击但是没有展开

——>

 

参考文献:

1. https://stackoverflow.com/questions/115983/how-can-i-add-an-empty-directory-to-a-git-repository

2. https://blog.csdn.net/fengchao2016/article/details/52769151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值