git 提交空目录

git 提交空目录

今天碰到这样一个问题:

项目中有一个目录—— uploads,专门放置上传的文件。但是这个目录中上传的文件,我不想同步到远程仓库,不想对其进行版本控制。

这个时候,有几种方式可以解决这个问题:

1. 添加一个 readme.md

可以在 .ignore 文件内添加两行代码,将 除了readme.md 之外的其他文件忽略掉

# 忽略uploads上传的所有文件
public/uploads/*
!public/uploads/README.md

通过 * 忽略所有文件,通过 !+文件名 不忽略指定文件。

2. 添加一个 .gitignore 文件到空目录内

朝空目录中添加一个 .gitignore 文件,文件内容为:

# 忽略文件夹内所有的文件
*

# 除了 .gitignore
!.gitignore

总结

其实两种方式的本质上都是一样的,都是在文件夹内指定一个不忽略的文件,然后就能把这个文件夹变相的上传到远程仓库,对这个文件夹的内容进行跟踪了。

从官方的 FAQ 页面中,我们可以发现,git 不支持提交空目录:

Can I add empty directories?

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

" and it will add the 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 (there is also a tool MarkEmptyDirs using the .NET framework which allows you to automate this task); you can leave it empty or fill in the names of files you do not expect to show up in the directory.

参考页面:git FQA

后记

其实很多技术疑问,在对应的官方网站都能找到解答,要善于从官方文档、官方博客、官方论坛寻找答案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值