git 提交空目录
- [1. git 无法感应空目录](#1. git 无法感应空目录)
- [2. git 提交空目录](#2. git 提交空目录)
- References
1. git 无法感应空目录
Git FAQ
https://archive.kernel.org/oldwiki/git.wiki.kernel.org/index.php/GitFaq.html
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.
目前 Git 索引 (暂存区) 的设计只允许列出文件。
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 the files in there.
在添加目录中的文件时,目录会被自动添加。目录永远不必添加到存储库中,也不会自行跟踪。git add <dir>
会添加目录中的文件。
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 do not expect to show up in the directory.
如果你真的需要一个目录存在,你应该在其中创建一个文件。.gitignore 可以很好地用于此目的,你可以将其留空或填写你不希望出现在目录中的文件名。
git 是用来索引文件的,它只关注文件,不关心目录。git 根据文件的内容去计算 hash 的,仅仅新增一个目录,git 是没办法处理的。
yongqiang@yongqiang:~/yongqiang_work/darknet$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ mkdir yongqiang
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/darknet$
2. git 提交空目录
需要在目录下创建 .gitkeep
或 .keep
文件,在项目的 .gitignore
中设置不忽略 .gitkeep
。
.gitkeep
或 .keep
是一个约定俗成的文件名并不会带有特殊规则。
strong@foreverstrong:~/gogsgit_work/parking_system$ mkdir tmp
strong@foreverstrong:~/gogsgit_work/parking_system$
strong@foreverstrong:~/gogsgit_work/parking_system$ cd tmp/
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$ ls
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$ touch .gitkeep
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$ ll
total 8
drwxrwxr-x 2 strong strong 4096 Jun 27 17:08 ./
drwxrwxr-x 8 strong strong 4096 Jun 27 17:07 ../
-rw-rw-r-- 1 strong strong 0 Jun 27 17:08 .gitkeep
strong@foreverstrong:~/gogsgit_work/parking_system/tmp$ cd ..
strong@foreverstrong:~/gogsgit_work/parking_system$
strong@foreverstrong:~/gogsgit_work/parking_system$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
tmp/
nothing added to commit but untracked files present (use "git add" to track)
strong@foreverstrong:~/gogsgit_work/parking_system$
strong@foreverstrong:~/gogsgit_work/parking_system$ git add .
strong@foreverstrong:~/gogsgit_work/parking_system$
strong@foreverstrong:~/gogsgit_work/parking_system$ git commit -m "add tmp"
strong@foreverstrong:~/gogsgit_work/parking_system$ git push
git 已经能感知到这个目录的存在了,其实是感应到里面那个 .gitkeep
或 .keep
文件的存在。
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/