git 提交空目录

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/

相关推荐

  1. git 提交目录

    2024-03-25 16:04:03       48 阅读
  2. Ubuntu下git提示:终止提交因为提交说明为

    2024-03-25 16:04:03       70 阅读
  3. 项目提交git仓库流程

    2024-03-25 16:04:03       112 阅读
  4. git 创建分支

    2024-03-25 16:04:03       39 阅读
  5. Git添加文件夹

    2024-03-25 16:04:03       29 阅读
  6. git 仓库笔记

    2024-03-25 16:04:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-25 16:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-25 16:04:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-25 16:04:03       82 阅读
  4. Python语言-面向对象

    2024-03-25 16:04:03       91 阅读

热门阅读

  1. flutter 单例模式

    2024-03-25 16:04:03       43 阅读
  2. 说一说promise是什么与使用方法

    2024-03-25 16:04:03       40 阅读
  3. 【Flutter学习笔记】10.1 自定义组件方法简介

    2024-03-25 16:04:03       38 阅读
  4. IOS面试题编程机制 36-40

    2024-03-25 16:04:03       39 阅读
  5. 「Linux系列」Shell 输入/输出重定向

    2024-03-25 16:04:03       43 阅读
  6. Flask蓝图找不到路由地址,访问404

    2024-03-25 16:04:03       39 阅读
  7. git 代码冲突处理

    2024-03-25 16:04:03       35 阅读